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/0.js
+8Lines changed: 8 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,6 @@
1
1
// Predict and explain first...
2
2
// =============> write your prediction here
3
+
// I predict that the error will be a SyntaxError because we are trying to declare a variable with the same name as a parameter. In JavaScript, you cannot declare a variable with the same name as a parameter within the same function scope. This will cause a SyntaxError because it creates a conflict in variable naming.
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
@@ -10,4 +11,11 @@ function capitalise(str) {
10
11
}
11
12
12
13
// =============> write your explanation here
14
+
// The original error happened because we tried to declare a new variable
15
+
// named `str` with `let` even though `str` was already a parameter. That's
16
+
// illegal in JavaScript and produces a SyntaxError. By removing the extra
17
+
// declaration (or by returning the string directly) the function works.
Copy file name to clipboardExpand all lines: Sprint-2/1-key-errors/1.js
+8-1Lines changed: 8 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
// Predict and explain first...
2
-
3
2
// Why will an error occur when this program runs?
4
3
// =============> write your prediction here
4
+
// I predict that the error will be a SyntaxError because we are trying to declare a variable with the same name as a parameter. In JavaScript, you cannot declare a variable with the same name as a parameter within the same function scope. This will cause a SyntaxError because it creates a conflict in variable naming.
5
5
6
6
// Try playing computer with the example to work out what is going on
Copy file name to clipboardExpand all lines: Sprint-2/1-key-errors/2.js
+7-2Lines changed: 7 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -4,17 +4,22 @@
4
4
// this function should square any number but instead we're going to get an error
5
5
6
6
// =============> write your prediction of the error here
7
+
// I predict that the error will be a SyntaxError because we are trying to declare a function with an invalid name. In JavaScript, function names cannot start with a number. By trying to declare a function named `square(3)`, we are violating this rule, which will result in a SyntaxError when the code is parsed.
8
+
7
9
8
10
functionsquare(3){
9
11
returnnum*num;
10
12
}
11
13
12
14
// =============> write the error message here
13
-
15
+
// SyntaxError due to an unexpected token or invalid function name.
14
16
// =============> explain this error message here
15
-
17
+
// This is because `square(3)` is not a valid function declaration in JavaScript, and the parser will not be able to understand it as a function definition.
// I predict that the error will be a ReferenceError because the function `multiply` does not return any value, so when we try to use it inside the template literal, it will return `undefined`. This will cause the output to be "The result of multiplying 10 and 32 is undefined" instead of the expected product of 10 and 32.
4
5
5
6
functionmultiply(a,b){
6
7
console.log(a*b);
@@ -9,6 +10,9 @@ function multiply(a, b) {
9
10
console.log(`The result of multiplying 10 and 32 is ${multiply(10,32)}`);
10
11
11
12
// =============> write your explanation here
12
-
13
+
// The error occurs because the `multiply` function does not return a value; it only logs the result to the console. When we use `multiply(10, 32)` inside the template literal, it evaluates to `undefined` since there is no return statement in the function. To fix this, we need to add a return statement to the `multiply` function so that it returns the product of `a` and `b` instead of just logging it.
// I predict that the error will be a ReferenceError because the function `sum` does not return any value, so when we try to use it inside the template literal, it will return `undefined`. This will cause the output to be "The sum of 10 and 32 is undefined" instead of the expected sum of 10 and 32.
4
4
functionsum(a,b){
5
5
return;
6
6
a+b;
@@ -9,5 +9,9 @@ function sum(a, b) {
9
9
console.log(`The sum of 10 and 32 is ${sum(10,32)}`);
10
10
11
11
// =============> write your explanation here
12
+
// The error occurs because the `sum` function does not return a value; it only has a return statement without any expression. When we use `sum(10, 32)` inside the template literal, it evaluates to `undefined` since there is no return value from the function. To fix this, we need to return the result of `a + b` instead of just having a return statement with no value.
Copy file name to clipboardExpand all lines: Sprint-2/2-mandatory-debug/2.js
+14-2Lines changed: 14 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,11 @@
1
1
// Predict and explain first...
2
-
2
+
// this function should square any number but instead we're going to get an error
3
3
// Predict the output of the following code:
4
4
// =============> Write your prediction here
5
-
5
+
// I predict that the output will be:
6
+
// The last digit of 42 is 3
7
+
// The last digit of 105 is 3
8
+
// The last digit of 806 is 3
6
9
constnum=103;
7
10
8
11
functiongetLastDigit(){
@@ -15,10 +18,19 @@ 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
20
// =============> write the output here
21
+
// The output will be:
22
+
// The last digit of 42 is 3
23
+
// The last digit of 105 is 3
24
+
// The last digit of 806 is 3
18
25
// Explain why the output is the way it is
19
26
// =============> write your explanation here
27
+
// The function `getLastDigit` is not taking any parameters, so it always returns the last digit of the global variable `num` (which is 103). This means that regardless of what number is passed to the function, it will always return 3.
20
28
// Finally, correct the code to fix the problem
21
29
// =============> write your new code here
30
+
functiongetLastDigit(num){
31
+
returnnum.toString().slice(-1);
32
+
}
22
33
23
34
// This program should tell the user the last digit of each number.
24
35
// Explain why getLastDigit is not working properly - correct the problem
36
+
// the function `getLastDigit` is not working properly because it does not take any parameters, and it always returns the last digit of the global variable `num`. To fix this, we need to modify the function to accept a parameter (the number we want to find the last digit of) and use that parameter instead of the global variable. This way, we can get the correct last digit for each number passed to the function.
Copy file name to clipboardExpand all lines: Sprint-2/4-mandatory-interpret/time-format.js
+5-1Lines changed: 5 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -18,17 +18,21 @@ function formatTimeDisplay(seconds) {
18
18
19
19
// a) When formatTimeDisplay is called how many times will pad be called?
20
20
// =============> write your answer here
21
+
// The `pad` function will be called three times when `formatTimeDisplay` is called, once for each of the time components: hours, minutes, and seconds. Each component is passed to the `pad` function to ensure it is displayed with at least two digits, adding a leading zero if necessary.
21
22
22
23
// Call formatTimeDisplay with an input of 61, now answer the following:
23
24
24
25
// b) What is the value assigned to num when pad is called for the first time?
25
26
// =============> write your answer here
27
+
// When `formatTimeDisplay(61)` is called, the first time `pad` is called, `num` is assigned the value of `totalHours`, which is 0.
26
28
27
29
// c) What is the return value of pad is called for the first time?
28
30
// =============> write your answer here
31
+
// The return value of `pad(0)` is "00".
29
32
30
33
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
31
34
// =============> write your answer here
32
-
35
+
// When `formatTimeDisplay(61)` is called, the last time `pad` is called, `num` is assigned the value of `remainingSeconds`, which 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
0 commit comments