11const penceString = "399p" ;
22
3- const penceStringWithoutTrailingP = penceString . substring ( 0 , penceString . length - 1 ) ;
3+ const penceStringWithoutTrailingP = penceString . substring (
4+ 0 ,
5+ penceString . length - 1 ,
6+ ) ;
47
58const paddedPenceNumberString = penceStringWithoutTrailingP . padStart ( 3 , "0" ) ;
6- const pounds = paddedPenceNumberString . substring ( 0 , paddedPenceNumberString . length - 2 ) ;
9+ const pounds = paddedPenceNumberString . substring (
10+ 0 ,
11+ paddedPenceNumberString . length - 2 ,
12+ ) ;
713
8- const pence = paddedPenceNumberString . substring ( paddedPenceNumberString . length - 2 ) ;
14+ const pence = paddedPenceNumberString . substring (
15+ paddedPenceNumberString . length - 2 ,
16+ ) ;
917
1018console . log ( `£${ pounds } .${ pence } ` ) ;
1119
@@ -21,7 +29,7 @@ console.log(`£${pounds}.${pence}`);
2129// Answers
2230
2331// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1):
24- // the substring() method returns the characters starting at index 0(3) up to and excluding the end(9) of penString variable.
32+ // the substring() method returns the characters starting at index 0(3) up to and excluding the end(9) of penString variable.
2533// This way only 399 is extracted of the string. Then the result is assigned to penceStringWithoutTrailingP variable.
2634
2735// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"):
@@ -33,8 +41,8 @@ console.log(`£${pounds}.${pence}`);
3341// This gives us only the pounds value and it the assigned to the pounds variable.
3442
3543// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"):
36- // substring method first returns the last two characters of the paddedPenceNumberString string.
44+ // substring method first returns the last two characters of the paddedPenceNumberString string.
3745// Then the padEnd() method adds a 0 if the length of the string is less than 2. Then it assigns it to pence variable.
3846
39- // 6. console.log(`£${pounds}.${pence}`):
47+ // 6. console.log(`£${pounds}.${pence}`):
4048// Finally console.log prints the pounds and pence adding the the pound sign £ first using template literal as £3.99
0 commit comments