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
+4-8Lines changed: 4 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -2,9 +2,9 @@
2
2
3
3
// Why will an error occur when this program runs?
4
4
// =============> write your prediction here
5
-
// It will crash for two reasons. 1. Trying to log a local variable 'decimalNumber' outside its function. 2. Trying to re-declare the parameter 'decimalNumber' using 'const' inside the function.
5
+
6
6
// Try playing computer with the example to work out what is going on
7
-
/* Commented out broken code:
7
+
8
8
functionconvertToPercentage(decimalNumber){
9
9
constdecimalNumber=0.5;
10
10
constpercentage=`${decimalNumber*100}%`;
@@ -13,12 +13,8 @@ function convertToPercentage(decimalNumber) {
13
13
}
14
14
15
15
console.log(decimalNumber);
16
-
*/
16
+
17
17
// =============> write your explanation here
18
-
// 'decimalNumber' is scoped to the function, so console.log outside can't see it (ReferenceError). Inside, re-declaring the parameter with 'const' causes a SyntaxError. Also, the function was never actually called.
//It will print "320", followed by "The result... is undefined". The function lacks a return value.
5
-
/* Commented out the broken code:
4
+
6
5
functionmultiply(a,b){
7
6
console.log(a*b);
8
7
}
9
8
10
9
console.log(`The result of multiplying 10 and 32 is ${multiply(10,32)}`);
11
-
*/
10
+
12
11
// =============> write your explanation here
13
-
// `console.log` just prints to the screen. Without a `return` statement, the function evaluates to `undefined`, which gets injected into the template literal.
12
+
14
13
// Finally, correct the code to fix the problem
15
14
// =============> write your new code here
16
-
functionmultiply(a,b){
17
-
returna*b;
18
-
}
19
-
console.log(`The result of multiplying 10 and 32 is ${multiply(10,32)}`);
// It will print "undefined". The 'return;' statement stops the function immediately before it even looks at 'a + b'.
4
-
/* Commented out the broken code:
3
+
5
4
functionsum(a,b){
6
5
return;
7
6
a+b;
8
7
}
9
8
10
9
console.log(`The sum of 10 and 32 is ${sum(10,32)}`);
11
-
*/
10
+
12
11
// =============> write your explanation here
13
-
// The 'return' keyword instantly exits the function. Since nothing is attached to it, it returns 'undefined'. The 'a + b;' line is dead code that never gets executed.
14
12
// Finally, correct the code to fix the problem
15
13
// =============> write your new code here
16
-
functionsum(a,b){
17
-
returna+b;
18
-
}
19
-
console.log(`The sum of 10 and 32 is ${sum(10,32)}`);
Copy file name to clipboardExpand all lines: Sprint-2/2-mandatory-debug/2.js
+3-13Lines changed: 3 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -2,8 +2,7 @@
2
2
3
3
// Predict the output of the following code:
4
4
// =============> Write your prediction here
5
-
// It will always print "3" for every log. The function ignores the numbers we pass in and blindly uses the global 'num' (103).
6
-
/* Commented out the broken code:
5
+
7
6
constnum=103;
8
7
9
8
functiongetLastDigit(){
@@ -13,22 +12,13 @@ function getLastDigit() {
13
12
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
14
13
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
15
14
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
16
-
*/
15
+
17
16
// Now run the code and compare the output to your prediction
18
17
// =============> 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
18
// Explain why the output is the way it is
23
19
// =============> write your explanation here
24
-
// The function lacks a parameter in its definition. Therefore, it ignores the arguments (42, 105, 806) and always falls back to the globally defined 'num' which is 103.
25
20
// Finally, correct the code to fix the problem
26
21
// =============> write your new code here
27
-
functiongetLastDigit(number){
28
-
returnnumber.toString().slice(-1);
29
-
}
30
-
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
31
-
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
32
-
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
22
+
33
23
// This program should tell the user the last digit of each number.
34
24
// Explain why getLastDigit is not working properly - correct the problem
0 commit comments