11const penceString = "399p" ;
22
3- const penceStringWithoutTrailingP = penceString . substring (
4- 0 ,
5- penceString . length - 1
6- ) ;
3+ const penceStringWithoutTrailingP = penceString . substring ( 0 , penceString . length - 1 ) ;
74
85const paddedPenceNumberString = penceStringWithoutTrailingP . padStart ( 3 , "0" ) ;
9- const pounds = paddedPenceNumberString . substring (
10- 0 ,
11- paddedPenceNumberString . length - 2
12- ) ;
6+ const pounds = paddedPenceNumberString . substring ( 0 , paddedPenceNumberString . length - 2 ) ;
137
14- const pence = paddedPenceNumberString
15- . substring ( paddedPenceNumberString . length - 2 )
16- . padEnd ( 2 , "0" ) ;
8+ const pence = paddedPenceNumberString . substring ( paddedPenceNumberString . length - 2 ) . padEnd ( 2 , "0" ) ;
179
1810console . log ( `£${ pounds } .${ pence } ` ) ;
1911
@@ -33,4 +25,16 @@ console.log(`£${pounds}.${pence}`);
3325// This way only 399 is extracted of the string. Then the result is assigned to penceStringWithoutTrailingP variable.
3426
3527// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"):
36- // The method padStart(3, "0") adds the string 0 before penceStringWithoutTrailingP only
28+ // The method padStart(3, "0") adds the string 0 before if the length of penceStringWithoutTrailingP string is less than 3 characters.
29+ // It then assign it to paddedPenceNumberString variable. For example if the string is 11, it returns 011 or the string is 1, it returns 001.
30+
31+ // 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2)
32+ // Again the method substring() returns the characters starting at length 0 and removing the last two characters which are the pence(00).
33+ // This gives us only the pounds value and it the assigned to the pounds variable.
34+
35+ // 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"):
36+ // substring method first returns the last two characters of the paddedPenceNumberString string.
37+ // Then the padEnd() method adds a 0 if the length of the string is less than 2. Then it assigns it to pence variable.
38+
39+ // 6. console.log(`£${pounds}.${pence}`):
40+ // Finally console.log prints the pounds and pence adding the the pound sign £ first using template literal as £3.99
0 commit comments