@@ -25,3 +25,37 @@ console.log(`£${pounds}.${pence}`);
2525
2626// To begin, we can start with
2727// 1. const penceString = "399p": initialises a string variable with the value "399p"
28+
29+ // ─────────────────────────────────────────────────────────────
30+ // STEP-BY-STEP BREAKDOWN:
31+ //
32+ // Line 1: const penceString = "399p";
33+ // Makes a variable that holds the price as text, with a "p" at the end
34+ // to show it's in pence.
35+ //
36+ // Lines 3-6: const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);
37+ // Cuts off the "p" at the end. It takes the text from the start up to
38+ // (but not including) the last letter. Now we have "399".
39+ //
40+ // Line 8: const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
41+ // Makes sure the text is at least 3 characters long. If it's shorter, it
42+ // adds "0" at the front. "399" is already 3 characters, so nothing changes.
43+ //
44+ // Lines 9-12: const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);
45+ // Takes everything except the last 2 characters. That's the pounds part.
46+ // For "399", this gives us "3".
47+ //
48+ // Lines 14-16: const pence = paddedPenceNumberString
49+ // .substring(paddedPenceNumberString.length - 2)
50+ // .padEnd(2, "0");
51+ // Takes the last 2 characters (that's the pence part). Then it makes sure
52+ // there are 2 characters by adding "0" at the end if needed. For "399",
53+ // this gives us "99".
54+ //
55+ // Line 18: console.log(`£${pounds}.${pence}`);
56+ // Prints the final price. The result is "£3.99".
57+ //
58+ // Why do we add extra "0"s (padding)?
59+ // So the price always looks right. Without it, a price like "5p" would
60+ // show up as "£0.5" (wrong) instead of "£0.05" (right).
61+ // ─────────────────────────────────────────────────────────────
0 commit comments