Skip to content

Commit e4ad14a

Browse files
committed
Fix: correct answers for questions a and b in 1-percentage-change.js
1 parent d90d611 commit e4ad14a

File tree

7 files changed

+57
-22
lines changed

7 files changed

+57
-22
lines changed

Sprint-1/3-mandatory-interpret/1-percentage-change.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ console.log(`The percentage change is ${percentageChange}`);
1212
// Read the code and then answer the questions below
1313

1414
// 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 4 and 5.
15+
// Answer: There are 5 function calls in total. They are located on lines 4, 5, and 10.
1616
// 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.
1818
// c) Identify all the lines that are variable reassignment statements
1919
// Answer: Lines 4 and 5.
2020
// d) Identify all the lines that are variable declarations

Sprint-2/1-key-errors/0.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3-
3+
// It will crash with a SyntaxError. We can't use 'let' to declare 'str' because 'str' is already taken by the function parameter.
44
// call the function capitalise with a string input
55
// interpret the error message and figure out why an error is occurring
6-
6+
/* I commented this out so it doesn't crash:
77
function capitalise(str) {
88
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
99
return str;
1010
}
11-
11+
*/
1212
// =============> write your explanation here
13+
//The error confirms "str has already been declared". Function parameters act as local variables, creating a naming conflict with `let str`.
1314
// =============> write your new code here
15+
function capitalise(str) {
16+
return `${str[0].toUpperCase()}${str.slice(1)}`;
17+
}
18+
console.log(capitalise("hello"));

Sprint-2/1-key-errors/1.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
// Why will an error occur when this program runs?
44
// =============> 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.
66
// Try playing computer with the example to work out what is going on
7-
7+
/* Commented out broken code:
88
function convertToPercentage(decimalNumber) {
99
const decimalNumber = 0.5;
1010
const percentage = `${decimalNumber * 100}%`;
@@ -13,8 +13,12 @@ function convertToPercentage(decimalNumber) {
1313
}
1414
1515
console.log(decimalNumber);
16-
16+
*/
1717
// =============> 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.
1919
// Finally, correct the code to fix the problem
2020
// =============> write your new code here
21+
function convertToPercentage(decimalNumber) {
22+
return `${decimalNumber * 100}%`;
23+
}
24+
console.log(convertToPercentage(0.5));

Sprint-2/1-key-errors/2.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,22 @@
44
// this function should square any number but instead we're going to get an error
55

66
// =============> write your prediction of the error here
7-
7+
// Syntax error. We put a hardcoded number '3' where a parameter name (like 'num') should be.
8+
/* Commented out the broken code so it doesn't crash:
89
function square(3) {
910
return num * num;
1011
}
11-
12+
*/
1213
// =============> write the error message here
13-
14+
// SyntaxError: Unexpected number
1415
// =============> explain this error message here
15-
16+
// Function definitions require variable names as parameters to act as placeholders, not literal values.
1617
// Finally, correct the code to fix the problem
1718

1819
// =============> write your new code here
19-
20+
function square(num) {
21+
return num * num;
22+
}
23+
console.log(square(3));
24+
console.log(square(4));
2025

Sprint-2/2-mandatory-debug/0.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
// Predict and explain first...
22

33
// =============> write your prediction here
4-
4+
//It will print "320", followed by "The result... is undefined". The function lacks a return value.
5+
/* Commented out the broken code:
56
function multiply(a, b) {
67
console.log(a * b);
78
}
89
910
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
10-
11+
*/
1112
// =============> 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.
1314
// Finally, correct the code to fix the problem
1415
// =============> write your new code here
16+
function multiply(a, b) {
17+
return a * b;
18+
}
19+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

Sprint-2/2-mandatory-debug/1.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3-
3+
// It will print "undefined". The 'return;' statement stops the function immediately before it even looks at 'a + b'.
4+
/* Commented out the broken code:
45
function sum(a, b) {
56
return;
67
a + b;
78
}
89
910
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
10-
11+
*/
1112
// =============> 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.
1214
// Finally, correct the code to fix the problem
1315
// =============> write your new code here
16+
function sum(a, b) {
17+
return a + b;
18+
}
19+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

Sprint-2/2-mandatory-debug/2.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
// Predict the output of the following code:
44
// =============> 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:
67
const num = 103;
78
89
function getLastDigit() {
@@ -12,13 +13,22 @@ function getLastDigit() {
1213
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
1314
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1415
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
15-
16+
*/
1617
// Now run the code and compare the output to your prediction
1718
// =============> 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
1822
// Explain why the output is the way it is
1923
// =============> 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.
2025
// Finally, correct the code to fix the problem
2126
// =============> write your new code here
22-
27+
function getLastDigit(number) {
28+
return number.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)}`);
2333
// This program should tell the user the last digit of each number.
2434
// Explain why getLastDigit is not working properly - correct the problem

0 commit comments

Comments
 (0)