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
Copy file name to clipboardExpand all lines: Sprint-2/1-key-errors/1.js
+22-2Lines changed: 22 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,12 @@
1
1
// Predict and explain first...
2
-
2
+
// syntax error identifier decimalNumber be already declare
3
+
// there is no function call and then you can't use the local variable decimalNumber out of the function
3
4
// Why will an error occur when this program runs?
5
+
//decimalNumber is already declared as a parameter, so declaring it again inside the function causes an error.
6
+
7
+
//There is no function call.
8
+
9
+
//You cannot use the local variable decimalNumber outside the function scope.
4
10
// =============> write your prediction here
5
11
6
12
// Try playing computer with the example to work out what is going on
@@ -11,10 +17,24 @@ function convertToPercentage(decimalNumber) {
11
17
12
18
returnpercentage;
13
19
}
14
-
15
20
console.log(decimalNumber);
16
21
17
22
// =============> write your explanation here
23
+
//The error occurred because a local variable was redeclared inside the function using a variable keyword. Since the parameter already acts as a local variable, redeclaring it caused an error.
24
+
25
+
//To fix this, I removed the variable keyword and modified the existing parameter instead.
26
+
27
+
//I also removed the unused variable from console.log because it was outside the function scope.
18
28
29
+
//Finally, I called the function directly inside console.log using convertToPercentage() so the function executes properly and returns the correct value.
// Predict and explain first BEFORE you run any code...
3
3
4
+
// The code has a syntax error because a number is used instead of a parameter name in the function definition.
5
+
// Additionally, num is used inside the function without being declared or passed as an argument, which causes a ReferenceError.
6
+
// If the function is not called, it will not execute.
4
7
// this function should square any number but instead we're going to get an error
5
8
6
9
// =============> write your prediction of the error here
7
10
11
+
// The code has a syntax error because a number is used instead of a parameter name in the function definition.
12
+
// Additionally, num is used inside the function without being declared or passed as an argument, which causes a ReferenceError.
13
+
// If the function is not called, it will not execute.
14
+
8
15
functionsquare(3){
9
16
returnnum*num;
10
17
}
11
18
19
+
20
+
12
21
// =============> write the error message here
22
+
// syntax error :unexpected number.
13
23
14
24
// =============> explain this error message here
25
+
// The error occurs because a number is used in the function definition instead of a parameter name. A function definition must contain a parameter (a variable name), not a value.
26
+
27
+
// Inside the function, num is used but it was never declared or passed as a parameter, which causes a ReferenceError because num is not defined in the scope.
28
+
29
+
// Also, if the function is not called, it will not execute.
15
30
16
31
// Finally, correct the code to fix the problem
32
+
// I corrected the function by properly defining num as a parameter instead of using a number in the function definition. This resolved the syntax error.
33
+
34
+
// Inside the function, num is now defined, so the ReferenceError is fixed.
17
35
36
+
// I then called the function with an argument and stored the returned value in a variable named num. Although the same variable name is used, the function parameter and the outer variable exist in different scopes, so there is no conflict.
// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
14
+
console.log(formatTimeDisplay(61))
15
+
16
+
// You will need to play computer with this example - use the Python Visualizer https://pythontutor.com/visualize.html#mode=edit
15
17
// to help you answer these questions
16
18
17
19
// Questions
18
20
19
21
// a) When formatTimeDisplay is called how many times will pad be called?
20
22
// =============> write your answer here
21
-
22
-
// Call formatTimeDisplay with an input of 61, now answer the following:
23
+
// ans: 3 times
24
+
// Call formatTimeDisplay with an input of 61, now answer the following:
23
25
24
26
// b) What is the value assigned to num when pad is called for the first time?
25
-
// =============> write your answer here
26
-
27
-
// c) What is the return value of pad is called for the first time?
28
-
// =============> write your answer here
27
+
// =============> write your answer here : 0
28
+
// c) What is the return value of pad is called for the first time?
29
+
// =============> write your answer here value : 00
29
30
30
31
// 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
32
-
32
+
// =============> write your answer here : 1 When formatTimeDisplay(61) is called, the value 61 is passed as the argument to seconds.
33
+
// Then remainingSeconds is calculated using seconds % 60, which gives 1.
34
+
// The last call to pad is pad(remainingSeconds).
35
+
// Since remainingSeconds is 1, the value assigned to num in the last call is 1.
33
36
// 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
+
// =============> write your answer here : 01 When formatTimeDisplay(61) is called, the value 1 is passed to pad during the last call because remainingSeconds equals 1.
38
+
// Inside the pad function:
39
+
// The number 1 is converted to a string.
40
+
// padStart(2, "0") ensures the string has at least two characters.
41
+
// Since "1" has only one character, a "0" is added to the beginning.
42
+
// So "1" becomes "01".
43
+
// This ensures the time format follows the 00:00:00 structure.
0 commit comments