@@ -25,3 +25,27 @@ 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+ // 2.1 removes the trailing 'p' from the string by taking a substring from index 0 to the second last character
31+ // 2.2 penceStringWithoutTrailingP now = "399"
32+
33+ // 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"):
34+ // 3.1 ensures the string has at least 3 characters by adding zeros to the start if necessary
35+ // 3.2 However, since "399" already has 3 characters, it remains unchanged
36+ // 3.3 paddedPenceNumberString now = "399"
37+
38+ // 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2):
39+ // 4.1 taking the first characters of the string up to the last two characters
40+ // 4.2 since "399" has only 3 characters, this only takes the first character
41+ // 4.3 pounds now = "3"
42+
43+ // 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"):
44+ // 5.1 takes the last two characters of the string to represent the pence
45+ // 5.2 since "399" has only 3 characters, this takes the last two characters "99"
46+ // 5.3 padEnd(2, "0") ensures that if there were less than 2 characters, it would add a "0" to the end
47+ // 5.4 pence now = "99"
48+
49+ // 6. console.log(`£${pounds}.${pence}`):
50+ // 6.1 constructs a formatted string representing the price in pounds and pence
51+ // 6.2 prints "£3.99" to the console
0 commit comments