1- const penceString = "399p " ;
1+ const penceString = "9p " ;
22
33const penceStringWithoutTrailingP = penceString . substring (
44 0 ,
@@ -25,3 +25,22 @@ 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+ // 3. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);
30+ // removes the trailing p so that there's a clean number format to work with.
31+
32+ // 8. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
33+ // pad the start so that there's always a character (0) in the pound's place, and the 10th and 100th place of the
34+ // fractional part (pence). E.g. for 9 pence, the pre-padded number would be "9", by padding it with 0, you get
35+ // 009, which can easily be formatted as £0.09.
36+
37+ // 9. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);
38+ // final two characters will always represent pence, be it "09" for 9 pence, or "90" for 90 pence
39+ // so the pound part will be anything preceding those two characters.
40+
41+ // 14. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
42+ // I guess, this is trying to ensure that pence always amounts to at least 2 digits, but it is unnecessary
43+ // as you're always taking the last two digits and padStart in line 8 ensures that the string always has at least 3 characters.
44+
45+ // 18. console.log(`£${pounds}.${pence}`);
46+ // this is just adding a pound symbol at the start and joining the pound and pence parts captured earlier with a dot.
0 commit comments