Skip to content

Commit f9cd0ae

Browse files
committed
Restore original questions alongside answers in Sprint-2 files; fix capitalise to use new const instead of reassigning parameter
1 parent bee1483 commit f9cd0ae

7 files changed

Lines changed: 102 additions & 30 deletions

File tree

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
1-
// Predict: SyntaxError or ReferenceError because `str` is re-declared with `let` inside the function
2-
// where it already exists as a parameter.
3-
// Explanation: You cannot use `let` to re-declare a variable that already exists in the same scope.
4-
// Fix: remove the `let` keyword — just reassign str.
1+
// Predict and explain first...
2+
// Prediction: SyntaxError because `str` is re-declared with `let` inside the function
3+
// where it already exists as a parameter. You cannot use `let` to re-declare a variable
4+
// that already exists in the same scope.
5+
6+
// call the function capitalise with a string input
7+
// interpret the error message and figure out why an error is occurring
8+
9+
// Original broken code:
10+
// function capitalise(str) {
11+
// let str = `${str[0].toUpperCase()}${str.slice(1)}`;
12+
// return str;
13+
// }
14+
15+
// Explanation: The parameter `str` already exists in the function scope.
16+
// Using `let str` tries to re-declare it, which is a SyntaxError.
17+
// Instead of reassigning `str` (which represents the original input),
18+
// we use a new const `capitalisedStr` to make it clear this is a different value.
519

620
function capitalise(str) {
7-
str = `${str[0].toUpperCase()}${str.slice(1)}`;
8-
return str;
21+
const capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`;
22+
return capitalisedStr;
923
}

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1+
// Predict and explain first...
2+
3+
// Why will an error occur when this program runs?
14
// Prediction: Two errors — (1) re-declaring decimalNumber with const inside the function
2-
// when it is already a parameter shadows and causes a SyntaxError.
5+
// when it is already a parameter causes a SyntaxError.
36
// (2) console.log(decimalNumber) outside the function will throw ReferenceError because
47
// decimalNumber is not defined in the outer scope.
5-
// Explanation: The parameter decimalNumber already exists; const re-declaration is illegal.
6-
// Also, decimalNumber is not accessible outside the function.
7-
// Fix: remove the const re-declaration inside the function, and pass a value to the function.
8+
9+
// Try playing computer with the example to work out what is going on
10+
11+
// Original broken code:
12+
// function convertToPercentage(decimalNumber) {
13+
// const decimalNumber = 0.5;
14+
// const percentage = `${decimalNumber * 100}%`;
15+
// return percentage;
16+
// }
17+
// console.log(decimalNumber);
18+
19+
// Explanation: The parameter decimalNumber already exists in the function scope;
20+
// const re-declaration is illegal. Also, decimalNumber is not accessible outside the function.
21+
// Fix: remove the const re-declaration inside the function, and pass a value when calling it.
822

923
function convertToPercentage(decimalNumber) {
1024
const percentage = `${decimalNumber * 100}%`;

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1+
// Predict and explain first BEFORE you run any code...
12

2-
// Prediction: SyntaxError — a function parameter must be a valid identifier, not a literal number.
3-
// Error: SyntaxError: Unexpected number
3+
// this function should square any number but instead we're going to get an error
4+
5+
// Prediction: SyntaxError — a function parameter must be a valid identifier, not a number literal.
6+
7+
// Original broken code:
8+
// function square(3) {
9+
// return num * num;
10+
// }
11+
12+
// Error message: SyntaxError: Unexpected number
413
// Explanation: `3` is a number literal and cannot be used as a parameter name.
14+
// Parameters must be valid identifiers (variable names).
515
// Fix: use a named parameter like `num`.
616

717
function square(num) {

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
// Predict and explain first...
12
// Prediction: The template literal will print "undefined" for the function result because
23
// multiply uses console.log internally but does not return a value (returns undefined).
4+
5+
// Original broken code:
6+
// function multiply(a, b) {
7+
// console.log(a * b);
8+
// }
9+
// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
10+
311
// Explanation: Functions without a return statement return undefined.
4-
// The outer console.log then interpolates undefined into the string.
12+
// The outer console.log then interpolates undefined into the string.
513
// Fix: replace console.log inside multiply with a return statement.
614

715
function multiply(a, b) {

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1+
// Predict and explain first...
12
// Prediction: The sum will print "undefined" because return; exits the function
23
// immediately without a value, so a + b is never evaluated.
3-
// Explanation: return; with no expression returns undefined. The a + b on the next
4-
// line is unreachable dead code.
4+
5+
// Original broken code:
6+
// function sum(a, b) {
7+
// return;
8+
// a + b;
9+
// }
10+
// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
11+
12+
// Explanation: return; with no expression returns undefined.
13+
// The a + b on the next line is unreachable dead code and is never executed.
514
// Fix: put a + b on the same line as return.
615

716
function sum(a, b) {

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1+
// Predict and explain first...
2+
3+
// Predict the output of the following code:
14
// Prediction: All three logs will print "3" (the last digit of the module-level const num = 103)
25
// because getLastDigit ignores its parameter and always uses the outer num.
3-
// Output:
6+
// Expected output:
47
// The last digit of 42 is 3
58
// The last digit of 105 is 3
69
// The last digit of 806 is 3
10+
11+
// Original broken code:
12+
// const num = 103;
13+
// function getLastDigit() {
14+
// return num.toString().slice(-1);
15+
// }
16+
17+
// Actual output matched prediction — all printed "3".
18+
719
// Explanation: The function has no parameter, so any argument passed in is discarded.
8-
// It always reads the outer `num` variable which is 103.
9-
// Fix: add a parameter to getLastDigit and use it.
20+
// It always reads the outer `num` variable which is 103.
21+
// Fix: add a parameter to getLastDigit and use it instead of the outer variable.
1022

1123
function getLastDigit(num) {
1224
return num.toString().slice(-1);

Sprint-2/4-mandatory-interpret/time-format.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,25 @@ function formatTimeDisplay(seconds) {
1515
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
1616
}
1717

18-
// a) pad is called 3 times per call to formatTimeDisplay (once for hours, minutes, seconds).
18+
// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
19+
// to help you answer these questions
1920

20-
// Call formatTimeDisplay(61):
21-
// remainingSeconds = 61 % 60 = 1
22-
// totalMinutes = (61 - 1) / 60 = 1
23-
// remainingMinutes = 1 % 60 = 1
24-
// totalHours = (1 - 1) / 60 = 0
25-
// pad is called with: totalHours=0, remainingMinutes=1, remainingSeconds=1
21+
// Questions
2622

27-
// b) num = 0 (totalHours) when pad is called for the first time.
23+
// a) When formatTimeDisplay is called how many times will pad be called?
24+
// =============> pad is called 3 times per call to formatTimeDisplay (once for totalHours, once for remainingMinutes, once for remainingSeconds).
2825

29-
// c) pad(0): "0".length < 2, so prepend "0" -> "00". Return value: "00"
26+
// Call formatTimeDisplay with an input of 61, now answer the following:
27+
// (remainingSeconds = 1, totalMinutes = 1, remainingMinutes = 1, totalHours = 0)
3028

31-
// d) num = 1 (remainingSeconds) when pad is called for the last time.
32-
// It is the last argument passed in the template literal.
29+
// b) What is the value assigned to num when pad is called for the first time?
30+
// =============> num = 0 (totalHours), because totalHours is the first argument in the template literal.
3331

34-
// e) pad(1): "1".length < 2, so prepend "0" -> "01". Return value: "01"
32+
// c) What is the return value of pad when it is called for the first time?
33+
// =============> "00" — "0".length < 2, so "0" is prepended, giving "00".
34+
35+
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
36+
// =============> num = 1 (remainingSeconds). It is the last argument passed in the template literal, so pad is called with it last.
37+
38+
// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
39+
// =============> "01" — "1".length < 2, so "0" is prepended, giving "01".

0 commit comments

Comments
 (0)