@@ -24,4 +24,27 @@ console.log(`£${pounds}.${pence}`);
2424// Try and describe the purpose / rationale behind each step
2525
2626// To begin, we can start with
27- // 1. const penceString = "399p": initialises a string variable with the value "399p"
27+ // 1. const penceString = "399p": initialises a string variable with the value "399p",p means pence so 399p means 399 pence.
28+
29+ // 2.const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);Removes the trailing "p" from the string.
30+ // .substring(0, length - 1) means: take all characters except the last one.Result: "399",which will get only the numeric part of the price.
31+
32+ // 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");Pads the numeric part from the left with zeros
33+ // until it's at least 3 characters long."399" stays as "399" (already 3 characters).if the number is 7, it becomes"007"
34+ // Purpose: Ensures consistent formatting so the last two digits are always the pence and the rest are pounds.
35+
36+ // 4.const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);Extracts the pounds part from the padded number
37+ // string.For "399", this gives: .substring(0, 1) → "3".Purpose: Get everything except the last two digits, which represent the pence.
38+
39+ // 5.const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");Extracts the last two characters
40+ // (the pence part) and ensures it's exactly two digits by padding on the right if needed.For "399" → .substring(1) = "99", no padding needed
41+ // For "3", it becomes "03". Purpose: Make sure the pence always shows as two digits.
42+
43+ // 6. console.log(£${pounds}.${pence});Constructs and logs a string in the proper pounds-and-pence format, like:text £3.99
44+ // Purpose: Output the final, human-readable currency value.
45+
46+
47+
48+
49+
50+
0 commit comments