@@ -25,3 +25,26 @@ 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+ // 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);
30+ // Removes trailing "p" from "399p" to get "399"
31+ // substring(0, 3) extracts characters from index 0 to 2
32+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring
33+
34+ // 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
35+ // Ensures string has at least 3 characters by adding leading zeros
36+ // "399" stays "399", but "9" would become "009" (for 9p = £0.09)
37+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
38+
39+ // 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);
40+ // Extracts all except last 2 characters for pounds
41+ // "399" → substring(0, 1) → "3" (£3)
42+
43+ // 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
44+ // Extracts last 2 characters for pence and pads to 2 digits
45+ // "399" → substring(1) → "99", then padEnd(2, "0") → "99"
46+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd
47+
48+ // 6. console.log(`£${pounds}.${pence}`);
49+ // Formats and displays result using template literal: "£3.99"
50+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
0 commit comments