Skip to content

Commit fb86516

Browse files
author
Pretty Taruvinga
committed
Fix truncated explanation in to-pounds step-by-step breakdown
1 parent a983056 commit fb86516

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed
Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
const penceString = "399p";
2+
// This line declares a constant variable named `penceString` and assigns it the value "399p".
3+
// This string represents a price in pence.
24

35
const penceStringWithoutTrailingP = penceString.substring(
46
0,
57
penceString.length - 1
68
);
9+
// This removes the trailing "p" from the string, leaving only the numeric value.
710

811
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
12+
// Ensures the number has at least 3 digits by adding leading zeros if necessary.
13+
914
const pounds = paddedPenceNumberString.substring(
1015
0,
1116
paddedPenceNumberString.length - 2
1217
);
18+
// Extracts the pounds part of the value.
1319

1420
const pence = paddedPenceNumberString
1521
.substring(paddedPenceNumberString.length - 2)
1622
.padEnd(2, "0");
23+
// Extracts the last two digits to represent the pence value.
1724

1825
console.log(${pounds}.${pence}`);
26+
// This line constructs a string in the format "£X.XX" where X represents the pounds and pence values, and then prints it to the console.
27+
// Prints the formatted price such as "£3.99".
1928

2029
// This program takes a string representing a price in pence
2130
// It then removes the trailing "p" character, pads the number with leading
@@ -24,7 +33,7 @@ console.log(`£${pounds}.${pence}`);
2433
// The program then builds up a string representing the price in pounds
2534
// and pence format (e.g., "£3.99") by concatenating the pounds and pence parts together with a "£" symbol and a decimal point. The final output is printed to the console.
2635

27-
// This program takes a string representing a price in pence (e.g., "399p") and converts it into a formatted string representing the price in pounds and pence (e.g., "£3.99").
36+
// This program takes a string representing a price in pence (e.g., "399p") and converts it into a formatted string representing the price in pounds and pence (e.g., "£3.99").
2837
// by concatenating the pounds and pence parts together with a "£" symbol and a decimal point. The final output is printed to the console.
2938

3039
// You need to do a step-by-step breakdown of each line in this program
@@ -33,21 +42,31 @@ console.log(`£${pounds}.${pence}`);
3342
// 2. penceStringWithoutTrailingP.padStart(3, "0") - line 5
3443
// 3. paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2) - line 6
3544
// 4. paddedPenceNumberString.substring(paddedPenceNumberString.length - 2) - line 9
36-
// 5. .padEnd(2, "0") - line 9
37-
// 6. console.log(`£${pounds}.${pence}`)' - line 11
45+
// 5. padEnd(2, "0") - line 9
46+
// 6. console.log(`£${pounds}.${pence}`) - line 11
3847

3948
// Try and describe the purpose / rationale behind each step
4049
// 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?
4150
// c) Identify all the lines that are variable reassignment statements
4251
// d) Identify all the lines that are variable declarations
4352
// e) Describe what the expression penceString.substring(0, penceString.length - 1) is doing - what is the purpose of this expression?
4453

45-
// To begin, we can start with
46-
// a) How many function calls are there in this file? Write down all the lines where a function call is made
54+
// a) How many function calls are there in this file?
4755

48-
// 1. penceString.substring(0, penceString.length - 1) - line 3
49-
// 1. const penceString = "399p": initialises a string variable with the val
50-
// ue "399p".
51-
// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1): creates a new string variable by taking a substring of `penceString` that excludes the last character (the "p"). This effectively removes the trailing "p" from the original string, leaving just the numeric part (e.g., "399").
52-
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): pads the `penceStringWithoutTrailingP` with leading zeros to ensure it has at least 3 characters. If `penceStringWithoutTrailingP` is shorter than 3 characters, it will add zeros at the start until it reaches a length of 3 (e.g., "399" remains "399", but "99" would become "099").
53-
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length -
56+
// 1. penceString.substring(0, penceString.length - 1)
57+
// Removes the trailing "p" from `penceString` by taking a substring that excludes the last character.
58+
59+
// 2. penceStringWithoutTrailingP.padStart(3, "0")
60+
// Ensures the string has at least 3 digits by adding leading zeros if needed.
61+
62+
// 3. paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2)
63+
// Extracts the pounds part by taking all digits except the last two.
64+
65+
// 4. paddedPenceNumberString.substring(paddedPenceNumberString.length - 2)
66+
// Extracts the last two digits to represent the pence value.
67+
68+
// 5. padEnd(2, "0")
69+
// Ensures the pence value always has two digits by adding trailing zeros if necessary.
70+
71+
// 6. console.log(`£${pounds}.${pence}`)
72+
// Prints the final formatted price in pounds and pence to the console.

0 commit comments

Comments
 (0)