Skip to content

Commit f4d4ae5

Browse files
author
Enice-Codes
committed
completed all excercises in sprint 2
1 parent fc76e95 commit f4d4ae5

10 files changed

Lines changed: 98 additions & 13 deletions

File tree

Sprint-2/1-key-errors/0.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// when running the code there will be a syntax error because the `str` being a function name has already been declared
4+
// so it cant be returned .so the return variable should have another name
35

46
// call the function capitalise with a string input
57
// interpret the error message and figure out why an error is occurring
8+
//Uncaught SyntaxError: Identifier 'str' has already been declared
9+
610

711
function capitalise(str) {
812
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
913
return str;
1014
}
1115

1216
// =============> write your explanation here
17+
// there is an error in the code because the `str` is declared twice and there are unexpected token '}'.
18+
1319
// =============> write your new code here
20+
function capitalise (str) {
21+
return `${str[0].toUpperCase()}${str.slice(1)}`;
22+
}
23+

Sprint-2/1-key-errors/1.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Predict and explain first...
2+
// the will be answer of the percentage printed on the console.log.because the function has been declared.
23

34
// Why will an error occur when this program runs?
45
// =============> write your prediction here
6+
//there will be a code error because of the function has been declared twice.
57

68
// Try playing computer with the example to work out what is going on
79

@@ -15,6 +17,13 @@ function convertToPercentage(decimalNumber) {
1517
console.log(decimalNumber);
1618

1719
// =============> write your explanation here
18-
20+
//when a function is called a value it given to the parameter.
1921
// Finally, correct the code to fix the problem
2022
// =============> write your new code here
23+
24+
function convertToPercentage(decimalNumber){
25+
const percentage = `${decimalNumber * 100 }%`;
26+
return percentage;
27+
}
28+
console.log (convertToPercentage(0.5)) //50
29+
console.log (convertToPercentage(0.25)) //25%

Sprint-2/1-key-errors/2.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11

22
// Predict and explain first BEFORE you run any code...
3-
3+
// the will be an error when this code runs because the retrun varaible has not been assigined by a value and there function has not declared a variable.
44
// this function should square any number but instead we're going to get an error
5-
65
// =============> write your prediction of the error here
6+
// synch equation
77

88
function square(3) {
99
return num * num;
1010
}
1111

1212
// =============> write the error message here
13+
// Uncaught SyntaxError: Unexpected number
14+
// > return num * num;
15+
// return num * num;
1316

14-
// =============> explain this error message here
17+
//Uncaught SyntaxError: Illegal return statement
1518

19+
// =============> explain this error message here
20+
// the error is simply saying that the return variable has not been assigned by a value.`illegal statement`.
1621
// Finally, correct the code to fix the problem
17-
1822
// =============> write your new code here
19-
20-
23+
function square (num){
24+
return num * num;
25+
}
26+
console.log (square(3)) // 9

Sprint-2/2-mandatory-debug/0.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
// Predict and explain first...
2-
32
// =============> write your prediction here
3+
// I predict that the function expression will run a nerro because the are errors in the code .such as the concole.log in line 6.
4+
45

56
function multiply(a, b) {
6-
console.log(a * b);
7+
concole.lpg( a* b);
8+
79
}
810

911
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
1012

1113
// =============> write your explanation here
12-
14+
// console.log prints out information it does not return any varable to its function.hence the syntax error.
15+
// console.log cant read unprovided information.
1316
// Finally, correct the code to fix the problem
1417
// =============> write your new code here
18+
function multiply (a,b){
19+
if (a===10 && b===32){
20+
let result =a * b ;
21+
}
22+
}

Sprint-2/2-mandatory-debug/1.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
//Js doesnt understand this code so it will read as an error because the return varable has not been aissinged any variable.
34

45
function sum(a, b) {
56
return;
@@ -9,5 +10,10 @@ function sum(a, b) {
910
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1011

1112
// =============> write your explanation here
13+
// js does not read values not assigned to any variable so the will be a syntax error not allowing the code to read .
1214
// Finally, correct the code to fix the problem
1315
// =============> write your new code here
16+
function sum (a ,b){
17+
return a + b ;
18+
}
19+
console.log(`The sum of 10 and 32 is ${sum(10.32)}`);

Sprint-2/2-mandatory-debug/2.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
// Predict and explain first...
2-
1+
// Predict and explain first..
32
// Predict the output of the following code:
43
// =============> Write your prediction here
4+
// i predict that the output will be 3 for all the numbers passed to the function because the function is not using the parameter
5+
// passed to it instead it is using the variable num which is 103 so it will always return 3 as the last digit of any number passed to it.
56

67
const num = 103;
78

@@ -15,10 +16,19 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1516

1617
// Now run the code and compare the output to your prediction
1718
// =============> write the output here
19+
// The last digit of 42 is 3
20+
// The last digit of 105 is 3
21+
// the last digit of 806 is 3
22+
1823
// Explain why the output is the way it is
1924
// =============> write your explanation here
25+
// The output is the way it is because the function getlastDigit is not using the parameter passed to it.instead it is using the variable num which is 103.
2026
// Finally, correct the code to fix the problem
2127
// =============> write your new code here
28+
// the function getLastDigit(num){
29+
// return num.string().slice(-1);}
2230

2331
// This program should tell the user the last digit of each number.
2432
// Explain why getLastDigit is not working properly - correct the problem
33+
// this is because the function getLastDigit is not using the parameter passed to it.
34+
// instead it is using the variable num which is 103. so it will always return 3 as the last digit of any number passed to it.s

Sprint-2/3-mandatory-implement/1-bmi.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@
1616

1717
function calculateBMI(weight, height) {
1818
// return the BMI of someone based off their weight and height
19-
}
19+
return Number((weight/(height *height)).toFixed(1));
20+
}
21+
22+
23+

Sprint-2/3-mandatory-implement/2-cases.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,10 @@
1414
// You will need to come up with an appropriate name for the function
1515
// Use the MDN string documentation to help you find a solution
1616
// This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase
17+
18+
function touppersneakers (input){
19+
return input.trim().replace(/\s+/g, "_").touppercase();
20+
}
21+
console.log(touppersneakers("hello there"));//Hello There
22+
console.log(touppersneakers("lord of the rings")); // lord of the rings
23+
console.log(touppersneakers("good morning")); // GOOD MORNING

Sprint-2/3-mandatory-implement/3-to-pounds.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,21 @@
44
// You will need to declare a function called toPounds with an appropriately named parameter.
55

66
// You should call this function a number of times to check it works for different inputs
7+
8+
9+
10+
function toSnakeCase(str) {
11+
const pecestringwithouttrailingp = pencestring.substring(0,pencestring.length -1);
12+
const padded penceNumberstring = pencestringWithoutTrailingP.padstart(3,"0");
13+
const pounds = paddedpenceNumberString.substring(0, paddedpencenumberstring.length-2);
14+
const pencr= paddedpencenumberstring.substring(paddedpencenumberstring.length -2)
15+
.padEnd(2,"0");
16+
return £${pounds}.${pence}`;
17+
}
18+
// test the function with different inputs
19+
consloe.log(topounds("399p"), // £3.99
20+
4console.log(toPounds("9p")); // £0.09
21+
console.log(toPounds("99p")); // £0.99
22+
console.log(toPounds("1234p")); // £12.34
23+
24+

Sprint-2/4-mandatory-interpret/time-format.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,24 @@ function formatTimeDisplay(seconds) {
2222

2323
// a) When formatTimeDisplay is called how many times will pad be called?
2424
// =============> write your answer here
25+
// pad will be called 3 times, once for totalHours, once for remainingMinutes, and once for remainingSeconds.
2526

2627
// Call formatTimeDisplay with an input of 61, now answer the following:
2728

2829
// b) What is the value assigned to num when pad is called for the first time?
2930
// =============> write your answer here
31+
// The value assigned to num when pad is called for the first time is 0, which is the value of totalHours when the input is 61 seconds.
3032

3133
// c) What is the return value of pad is called for the first time?
3234
// =============> write your answer here
35+
// The return value of pad when it is called for the first time is "00", which is the padded string representation of totalHours (0) when the input is 61 seconds.
3336

3437
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
3538
// =============> write your answer here
39+
// The value assigned to num when pad is called for the last time in this program is 1, which is the value of remainingSeconds when the input is 61 seconds.
40+
// This is because 61 seconds is equal to 1 minute and 1 second, so the remaining seconds after calculating total minutes and hours is 1.
3641

3742
// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
3843
// =============> write your answer here
44+
// The return value of pad when it is called for the last time in this program is "01", which is the padded string representation of remainingSeconds (1)
45+
// when the input is 61 seconds.

0 commit comments

Comments
 (0)