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-1/3-mandatory-interpret/1-percentage-change.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -12,9 +12,9 @@ console.log(`The percentage change is ${percentageChange}`);
12
12
// Read the code and then answer the questions below
13
13
14
14
// a) How many function calls are there in this file? Write down all the lines where a function call is made
15
-
// Answer: There are 2 function calls in total. They are both `Number()` and are located on lines 4and 5.
15
+
// Answer: There are 5 function calls in total. They are located on lines 4, 5, and 10.
16
16
// b) Run the code and identify the line where the error is coming from - why is this error occurring? How can you fix this problem?
17
-
// Answer: The error comes from line 5. It occurs because we are trying to reassign a value to `priceAfterOneYear`, which was originally declared as a `const`. To fix it, change `const` to `let` on line 2.
17
+
// Answer: The error comes from line 4. Since JavaScript executes from top to bottom, it crashes on line 4 because we are trying to reassign a new value to `carPrice`, which was originally declared as a `const`. To fix it, change `const` to `let` on lines 1 and 2.
18
18
// c) Identify all the lines that are variable reassignment statements
19
19
// Answer: Lines 4 and 5.
20
20
// d) Identify all the lines that are variable declarations
Copy file name to clipboardExpand all lines: Sprint-2/1-key-errors/1.js
+8-4Lines changed: 8 additions & 4 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
-
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.
6
6
// Try playing computer with the example to work out what is going on
7
-
7
+
/* Commented out broken code:
8
8
function convertToPercentage(decimalNumber) {
9
9
const decimalNumber = 0.5;
10
10
const percentage = `${decimalNumber * 100}%`;
@@ -13,8 +13,12 @@ function convertToPercentage(decimalNumber) {
13
13
}
14
14
15
15
console.log(decimalNumber);
16
-
16
+
*/
17
17
// =============> write your explanation here
18
-
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:
5
6
function multiply(a, b) {
6
7
console.log(a * b);
7
8
}
8
9
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
+
// `console.log` just prints to the screen. Without a `return` statement, the function evaluates to `undefined`, which gets injected into the template literal.
13
14
// Finally, correct the code to fix the problem
14
15
// =============> 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:
4
5
function sum(a, b) {
5
6
return;
6
7
a + b;
7
8
}
8
9
9
10
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
10
-
11
+
*/
11
12
// =============> 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.
12
14
// Finally, correct the code to fix the problem
13
15
// =============> 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
+13-3Lines changed: 13 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,8 @@
2
2
3
3
// Predict the output of the following code:
4
4
// =============> Write your prediction here
5
-
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:
6
7
const num = 103;
7
8
8
9
function getLastDigit() {
@@ -12,13 +13,22 @@ function getLastDigit() {
12
13
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
13
14
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
14
15
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
15
-
16
+
*/
16
17
// Now run the code and compare the output to your prediction
17
18
// =============> 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
18
22
// Explain why the output is the way it is
19
23
// =============> 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.
20
25
// Finally, correct the code to fix the problem
21
26
// =============> write your new code here
22
-
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)}`);
23
33
// This program should tell the user the last digit of each number.
24
34
// Explain why getLastDigit is not working properly - correct the problem
0 commit comments