Skip to content

Commit e62d2f2

Browse files
Update Sprint-1 answers based on reviewer feedback: use increment term, interval notation, type conversion, fix syntax error explanation, and clarify padEnd redundancy
1 parent c7273b2 commit e62d2f2

5 files changed

Lines changed: 56 additions & 21 deletions

File tree

Sprint-1/1-key-exercises/1-count.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ count = count + 1;
44

55
// Line 1 is a variable declaration, creating the count variable with an initial value of 0
66
// Describe what line 3 is doing, in particular focus on what = is doing
7-
// The code adds 1 to the count variable.
7+
// Line 3 is an increment operation. The = operator is the assignment operator:
8+
// it evaluates the expression on the right (count + 1), then assigns the result back to count.

Sprint-1/1-key-exercises/4-random.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,11 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
99
// Try logging the value of num and running the program several times to build an idea of what the program is doing
1010

1111
console.log(num);
12-
// num represents a random integer between 1 and 100.
12+
13+
// num represents a random integer in the range [1, 100].
14+
// Breakdown:
15+
// 1. Math.random() returns a decimal number in the range [0, 1).
16+
// 2. (maximum - minimum + 1) calculates the size of the range, which is 100.
17+
// 3. Math.random() * 100 gives a decimal in the range [0, 100).
18+
// 4. Math.floor() rounds down to the nearest integer, giving a value in the range [0, 99].
19+
// 5. Adding minimum (1) shifts the range to [1, 100].

Sprint-1/2-mandatory-errors/3.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
const cardNumber = "4533787178994213";
2-
const last4Digits = cardNumber.slice(-4);
1+
const cardNumber = 4533787178994213;
2+
const last4Digits = String(cardNumber).slice(-4);
33

44
// The last4Digits variable should store the last 4 digits of cardNumber
55
// However, the code isn't working
66
// Before running the code, make and explain a prediction about why the code won't work
77
// Then run the code and see what error it gives.
88
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
99
// Then try updating the expression last4Digits is assigned to, in order to get the correct value
10+
11+
// Prediction: The original code will throw a TypeError because .slice() is a string method,
12+
// and cardNumber is a number, not a string. Numbers do not have a .slice() method.
13+
// Fix: We use String(cardNumber) to convert the number to a string first,
14+
// then call .slice(-4) to extract the last 4 characters: "4213".
Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
let carPrice = "10,000";
22
let priceAfterOneYear = "8,543";
33

4-
carPrice = Number(carPrice.replace(/,/g, ""));
5-
priceAfterOneYear = Number(priceAfterOneYear.replace(/,/g, ""));
4+
carPrice = Number(carPrice.replaceAll(",", ""));
5+
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
66
const priceDifference = carPrice - priceAfterOneYear;
77
const percentageChange = (priceDifference / carPrice) * 100;
88

@@ -11,12 +11,24 @@ console.log(`The percentage change is ${percentageChange}`);
1111
// Read the code and then answer the questions below
1212

1313
// a) How many function calls are there in this file? Write down all the lines where a function call is made
14-
// There are 5 function calls: Number() twice, replaceAll() twice, and console.log().
14+
// There are 5 function calls:
15+
// Line 4: replaceAll(",", "") and Number()
16+
// Line 5: replaceAll(",", "") and Number()
17+
// Line 9: console.log()
18+
1519
// 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?
16-
// replaceAll might not work in older Node versions
20+
// The original code on line 5 was: priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
21+
// This causes a SyntaxError because there is a missing comma between the two arguments of replaceAll().
22+
// The original code had replaceAll("," "") but it should be replaceAll(",", "").
23+
// The comma between "," and "" is needed to separate the two arguments of the function.
24+
// The fix is simply adding the missing comma: replaceAll(",", "")
25+
1726
// c) Identify all the lines that are variable reassignment statements
18-
// Lines 4 and 5 are variable reassignment statements.
27+
// Lines 4 and 5 are variable reassignment statements (carPrice and priceAfterOneYear are reassigned using let).
28+
1929
// d) Identify all the lines that are variable declarations
20-
// Lines 1, 2, 7, and 8 are variable declarations.
30+
// Lines 1, 2, 6, 7 are variable declarations (using let or const).
31+
2132
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
22-
// It removes the comma from the string and converts it into a Number type.
33+
// It removes all commas from the string "10,000" to get "10000",
34+
// then converts that string into the number 10000 using Number().

Sprint-1/3-mandatory-interpret/3-to-pounds.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,29 @@ console.log(`£${pounds}.${pence}`);
2020
// Step-by-step breakdown:
2121

2222
// 1. const penceString = "399p";
23-
// Initialises a string variable with the value "399p".
23+
// Declares a string variable with the value "399p".
2424

25-
// 2. const penceStringWithoutTrailingP = ...
26-
// It removes the last character ('p') from the string to keep only the numbers.
25+
// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);
26+
// Removes the trailing 'p' character, resulting in "399".
2727

28-
// 3. const paddedPenceNumberString = ...
29-
// It ensures the string has at least 3 characters by adding a "0" at the beginning if needed (e.g., "50" becomes "050").
28+
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
29+
// Pads the string to at least 3 characters with leading zeros. "399" stays "399", but "5" would become "005".
3030

31-
// 4. const pounds = ...
32-
// It extracts the pounds part by taking all characters except the last two.
31+
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);
32+
// Extracts all characters except the last two to get the pounds part. "399" becomes "3".
3333

34-
// 5. const pence = ...
35-
// It extracts the last two characters to represent the pence part and ensures it has two digits.
34+
// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
35+
// Extracts the last two characters for the pence part and pads with trailing "0" if needed.
3636

3737
// 6. console.log(`£${pounds}.${pence}`);
38-
// It prints the final result in the currency format: £3.99.
38+
// Outputs the formatted currency: £3.99
39+
40+
// Question: Do we really need .padEnd(2, "0") in this script?
41+
// No, we do not actually need .padEnd(2, "0") in this script.
42+
// Because .padStart(3, "0") already guarantees the string has at least 3 characters,
43+
// .substring(length - 2) will always extract exactly 2 characters for the pence part.
44+
// For example:
45+
// "5p" -> "5" -> padStart -> "005" -> pence = "05" (already 2 chars, padEnd does nothing)
46+
// "50p" -> "50" -> padStart -> "050" -> pence = "50" (already 2 chars, padEnd does nothing)
47+
// "399p" -> "399" -> padStart -> "399" -> pence = "99" (already 2 chars, padEnd does nothing)
48+
// So .padEnd(2, "0") is redundant here because padStart already ensures enough characters.

0 commit comments

Comments
 (0)