@@ -2,13 +2,13 @@ const penceString = "399p";
22
33const penceStringWithoutTrailingP = penceString . substring (
44 0 ,
5- penceString . length - 1
5+ penceString . length - 1 ,
66) ;
77
88const paddedPenceNumberString = penceStringWithoutTrailingP . padStart ( 3 , "0" ) ;
99const pounds = paddedPenceNumberString . substring (
1010 0 ,
11- paddedPenceNumberString . length - 2
11+ paddedPenceNumberString . length - 2 ,
1212) ;
1313
1414const pence = paddedPenceNumberString
@@ -25,3 +25,15 @@ 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+ // Step-by-step breakdown:
30+ //const penceString = "399p"; creates a string containing the amount in pence, including the letter p.
31+ /*const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);
32+ removes the final p so the string becomes "399".*/
33+ /*const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
34+ makes sure the number has at least 3 characters by adding leading zeros if needed.*/
35+ /*const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);
36+ takes the part before the last 2 digits, which is the pounds value.*/
37+ /*const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
38+ takes the last 2 digits, which are the pence value, and makes sure they are 2 digits long.*/
39+ /*console.log(`£${pounds}.${pence}`); prints the final money value in pounds format, like£3.99`.*/
0 commit comments