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
// Prediction: There will be an error because we are trying to declare a variable 'str' using 'let', but 'str' is already declared as the function's parameter.
3
4
4
5
// call the function capitalise with a string input
5
6
// interpret the error message and figure out why an error is occurring
7
+
capitalise("mahmoud");
6
8
7
9
functioncapitalise(str){
8
-
letstr=`${str[0].toUpperCase()}${str.slice(1)}`;
9
-
returnstr;
10
+
// We fix the error by just updating the existing parameter without using 'let',
11
+
// or by creating a new variable with a DIFFERENT name. Here we use a different name.
// Why will an error occur when this program runs?
4
-
// =============> write your prediction here
5
-
6
-
// Try playing computer with the example to work out what is going on
4
+
// ===========> write your prediction here
5
+
// Prediction: There are two errors. First, we are trying to redeclare the parameter 'decimalNumber' using 'const'. Second, we are trying to log 'decimalNumber' outside the function where it doesn't exist.
7
6
8
7
functionconvertToPercentage(decimalNumber){
9
-
constdecimalNumber=0.5;
8
+
// We removed 'const decimalNumber = 0.5;' to use the parameter directly
10
9
constpercentage=`${decimalNumber*100}%`;
11
-
12
10
returnpercentage;
13
11
}
14
12
15
-
console.log(decimalNumber);
16
-
17
-
// =============> write your explanation here
13
+
// ===========> write your explanation here
14
+
// Explanation: A function parameter cannot be redeclared using 'const'. Also, parameters are local variables, meaning they cannot be accessed outside the function globally.
18
15
19
16
// Finally, correct the code to fix the problem
20
-
// =============> write your new code here
17
+
// ===========> write your new code here
18
+
// We fix this by calling the function properly and passing 0.5 as an argument inside console.log.
// Predict and explain first BEFORE you run any code...
3
2
4
3
// this function should square any number but instead we're going to get an error
5
4
6
-
// =============> write your prediction of the error here
5
+
// ===========> write your prediction of the error here
6
+
// Prediction: We will get a SyntaxError because a function parameter cannot be a literal number (like 3). It must be a valid variable name (identifier).
7
7
8
-
functionsquare(3){
9
-
returnnum*num;
8
+
functionsquare(num){
9
+
returnnum*num;
10
10
}
11
11
12
-
// =============> write the error message here
12
+
// ===========> write the error message here
13
+
// Error message: SyntaxError: Unexpected number
13
14
14
-
// =============> explain this error message here
15
+
// ===========> explain this error message here
16
+
// Explanation: When declaring a function, the parameters must be names (like 'num'), not actual values. We only pass actual values (like 3) when we CALL the function.
15
17
16
18
// Finally, correct the code to fix the problem
17
19
18
-
// =============> write your new code here
19
-
20
-
20
+
// ===========> write your new code here
21
+
// The fixed code is written above where I changed '3' to 'num'.
// Prediction: The output sentence will say "... is undefined" because the multiply function does not return a value.
4
5
5
6
functionmultiply(a,b){
6
-
console.log(a*b);
7
+
// We removed console.log and added 'return' so the function gives the result back
8
+
returna*b;
7
9
}
8
10
9
11
console.log(`The result of multiplying 10 and 32 is ${multiply(10,32)}`);
10
12
11
-
// =============> write your explanation here
13
+
// ===========> write your explanation here
14
+
// Explanation: Without a 'return' statement, a function evaluates to 'undefined'. To use the result of the calculation inside the template literal string, the function MUST return it.
12
15
13
16
// Finally, correct the code to fix the problem
14
-
// =============> write your new code here
17
+
// ===========> write your new code here
18
+
// The fixed code is written above. We changed 'console.log(a * b)' to 'return a * b'.
// Prediction: The output will say "... is undefined" because the function returns nothing before it even calculates the sum.
3
4
4
5
functionsum(a,b){
5
-
return;
6
-
a+b;
6
+
// We fix the error by putting the expression on the SAME line as the 'return' keyword.
7
+
returna+b;
7
8
}
8
9
9
10
console.log(`The sum of 10 and 32 is ${sum(10,32)}`);
10
11
11
-
// =============> write your explanation here
12
+
// ===========> write your explanation here
13
+
// Explanation: In JavaScript, if you put a line break immediately after the 'return' keyword, it acts as 'return;' and stops the function, returning 'undefined'. The expression 'a + b' must be on the same line.
14
+
12
15
// Finally, correct the code to fix the problem
13
-
// =============> write your new code here
16
+
// ===========> write your new code here
17
+
// The fixed code is written above. I moved 'a + b' to the same line as 'return'.
// Prediction: The output will incorrectly say the last digit is "3" for all numbers, because the function uses the global variable 'num' (103) instead of accepting a parameter.
5
6
6
-
constnum=103;
7
+
// We don't need this global variable, so I commented it out:
8
+
// const num = 103;
7
9
8
-
functiongetLastDigit(){
10
+
// We added 'num' as a parameter inside the parentheses
11
+
functiongetLastDigit(num){
9
12
returnnum.toString().slice(-1);
10
13
}
11
14
@@ -14,11 +17,13 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
14
17
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
15
18
16
19
// Now run the code and compare the output to your prediction
17
-
// =============> write the output here
20
+
// ===========> write the output here
21
+
// Output after the fix: 2, 5, 6. (Before the fix it was 3, 3, 3).
22
+
18
23
// Explain why the output is the way it is
19
-
// =============> write your explanation here
20
-
// Finally, correct the code to fix the problem
21
-
// =============> write your new code here
24
+
// ===========> write your explanation here
25
+
// Explanation: The original function had no parameters, so it used the global variable 'num = 103'. By adding 'num' as a parameter, the function now correctly uses the value passed into it when called.
22
26
23
-
// This program should tell the user the last digit of each number.
24
-
// Explain why getLastDigit is not working properly - correct the problem
// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
15
-
// to help you answer these questions
16
-
14
+
// You will need to play computer with this example
17
15
// Questions
18
16
19
17
// a) When formatTimeDisplay is called how many times will pad be called?
20
-
// =============> write your answer here
18
+
// ===========> write your answer here
19
+
// Answer: 3 times (once for hours, once for minutes, and once for seconds).
21
20
22
21
// Call formatTimeDisplay with an input of 61, now answer the following:
22
+
// (I am calling it here to see the result)
23
+
console.log(formatTimeDisplay(61));
23
24
24
25
// b) What is the value assigned to num when pad is called for the first time?
25
-
// =============> write your answer here
26
+
// ===========> write your answer here
27
+
// Answer: 0. Because JavaScript evaluates the string from left to right, so it calls pad(totalHours) first, and totalHours is 0.
26
28
27
29
// c) What is the return value of pad is called for the first time?
28
-
// =============> write your answer here
30
+
// ===========> write your answer here
31
+
// Answer: "00"
29
32
30
-
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
31
-
// =============> write your answer here
33
+
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
34
+
// ===========> write your answer here
35
+
// Answer: 1. Explanation: The last call in the return statement is pad(remainingSeconds). When the input is 61 seconds, 61 % 60 leaves 1 remaining second.
32
36
33
-
// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
34
-
// =============> write your answer here
37
+
// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
38
+
// ===========> write your answer here
39
+
// Answer: "01". Explanation: The pad function takes the number 1, converts it to a string "1", and pads the start with a "0" to make it 2 characters long ("01").
0 commit comments