You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// ==============> This will throw a SyntaxError, because 'str' is already declared as the function's parameter, and line 8 tries to declare a new variable with the same name using let.
3
3
4
4
// call the function capitalise with a string input
5
5
// interpret the error message and figure out why an error is occurring
// ==============> The error is "SyntaxError: Identifier 'str' has already been declared". This happens because you can't declare a new variable with let using a name that's already taken - in this case, the parameter str.
12
+
// ==============> function capitalise(str) {
13
+
// let capitalisedStr = ${str[0].toUpperCase()}${str.slice(1)};
// Why will an error occur when this program runs?
4
-
// =============> write your prediction here
4
+
// ==============> This will throw a SyntaxError because decimalNumber is already declared as the function's parameter, and line 9 tries to redeclare it with const.
5
5
6
-
// Try playing computer with the example to work out what is going on
6
+
// Try playing computer with the example to work out what will happen
7
7
8
8
functionconvertToPercentage(decimalNumber){
9
-
constdecimalNumber=0.5;
10
9
constpercentage=`${decimalNumber*100}%`;
11
-
12
10
returnpercentage;
13
11
}
14
12
15
-
console.log(decimalNumber);
13
+
console.log(convertToPercentage(0.5));
16
14
17
-
// =============> write your explanation here
15
+
// ==============> The error is "SyntaxError: Identifier 'decimalNumber' has already been declared". It happens for the same reason as before - you can't redeclare a variable with the same name as an existing function parameter using const.
18
16
19
17
// Finally, correct the code to fix the problem
20
-
// =============> write your new code here
18
+
// ==============> function convertToPercentage(decimalNumber) {
// Predict and explain first BEFORE you run any code.
1
2
2
-
// Predict and explain first BEFORE you run any code...
3
+
// this function should square any number but instead
3
4
4
-
// this function should square any number but instead we're going to get an error
5
+
// ==============> This will throw a SyntaxError, because 3 is used as the parameter name in function square(3), but parameter names can't be numbers - they need to be valid identifiers like "num".
5
6
6
-
// =============> write your prediction of the error here
7
-
8
-
functionsquare(3){
9
-
returnnum*num;
7
+
functionsquare(num){
8
+
returnnum*num;
10
9
}
11
10
12
-
// =============> write the error message here
11
+
// ==============> The error is "SyntaxError: Unexpected number"
13
12
14
-
// =============> explain this error message here
13
+
// ==============> This happens because 3 is a number, not a valid parameter name. Parameter names must be identifiers, like "num" - JavaScript doesn't know what to do with a number in that position.
// ==============> This will print "320" first (from inside the function), then print "The result of multiplying 10 and 32 is undefined", because multiply() doesn't return a value - it only logs it.
4
4
5
5
functionmultiply(a,b){
6
-
console.log(a*b);
6
+
returna*b;
7
7
}
8
8
9
9
console.log(`The result of multiplying 10 and 32 is ${multiply(10,32)}`);
10
10
11
-
// =============> write your explanation here
11
+
// ==============> The function was using console.log to display the answer, instead of returning it. This meant when the outer console.log tried to use the value from multiply(10, 32), it got undefined instead of the actual number, since the function returned nothing.
// ==============> This will print "The sum of 10 and 32 is undefined", because line 5 has a bare "return;" with nothing after it - this immediately exits the function and returns undefined. Line 6 (a + b) never runs.
3
3
4
4
functionsum(a,b){
5
-
return;
6
-
a+b;
5
+
returna+b;
7
6
}
8
7
9
8
console.log(`The sum of 10 and 32 is ${sum(10,32)}`);
10
9
11
-
// =============> write your explanation here
10
+
// ==============> The function had "return;" on its own line, followed by "a + b;" on the next line. Once JavaScript hits return; with nothing after it, the function ends immediately and returns undefined - the a + b line is unreachable and never executes.
// ==============> All three lines will print "3", because getLastDigit() ignores the number passed in and always uses the outer variable num (103), whose last digit is 3.
5
5
6
6
constnum=103;
7
7
8
-
functiongetLastDigit(){
8
+
functiongetLastDigit(num){
9
9
returnnum.toString().slice(-1);
10
10
}
11
11
@@ -14,11 +14,13 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
14
14
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
15
15
16
16
// Now run the code and compare the output to your prediction
17
-
// =============> write the output here
17
+
// ==============> The output was "3", "3", "3" as predicted.
18
18
// Explain why the output is the way it is
19
-
// =============> write your explanation here
19
+
// ==============> getLastDigit didn't accept a parameter, so it always used the outer num variable (103) instead of the number passed in when calling the function.
20
20
// Finally, correct the code to fix the problem
21
-
// =============> write your new code here
21
+
// ==============> function getLastDigit(num) {
22
+
// return num.toString().slice(-1);
23
+
// }
22
24
23
25
// This program should tell the user the last digit of each number.
24
26
// Explain why getLastDigit is not working properly - correct the problem
0 commit comments