11const penceString = "399p" ;
22
3- const penceStringWithoutTrailingP = penceString . substring (
4- 0 ,
5- penceString . length - 1
6- ) ;
3+ const penceStringWithoutTrailingP =
4+ penceString . substring ( 0 , penceString . length - 1 ) ; // => "399";
75
8- const paddedPenceNumberString = penceStringWithoutTrailingP . padStart ( 3 , "0" ) ;
9- const pounds = paddedPenceNumberString . substring (
10- 0 ,
11- paddedPenceNumberString . length - 2
12- ) ;
6+ const paddedPenceNumberString = penceStringWithoutTrailingP . padStart ( 3 , "0" ) ; // => "399";
7+ const pounds = paddedPenceNumberString . substring ( 0 , paddedPenceNumberString . length - 2 ) ; // => "3"
138
149const pence = paddedPenceNumberString
15- . substring ( paddedPenceNumberString . length - 2 )
16- . padEnd ( 2 , "0" ) ;
10+ . substring ( paddedPenceNumberString . length - 2 ) // => "99"
11+ . padEnd ( 2 , "0" ) ;
1712
18- console . log ( `£${ pounds } .${ pence } ` ) ;
13+ console . log ( `£${ pounds } .${ pence } ` ) ; // => "3.99"
1914
2015// This program takes a string representing a price in pence
2116// The program then builds up a string representing the price in pounds
@@ -24,4 +19,12 @@ console.log(`£${pounds}.${pence}`);
2419// Try and describe the purpose / rationale behind each step
2520
2621// To begin, we can start with
27- // 1. const penceString = "399p": initialises a string variable with the value "399p"
22+ // 1. const penceString = "399p": initializes a string variable with the value "399p"
23+ // 2. const penceStringWithoutTrailingP uses the substring()method to remove the last character.
24+ // 3. const paddedPenceNumberString tries to add "0" at the beginning, but the length is already 3; so it does nothing.
25+ // Its worthy to talk about padStart() method. we use this to add a character at the beginning of a string.
26+ // 4. const pound uses the substring() method on paddedPenceNumberString variable and remove its last two characters.Its value ll be "3".
27+ // 5. const pence uses substring() on paddedPenceNumberString variable and take its length and then subtract it with 2 ...
28+ // which will remain like this .substring(1), so it take the index 1 till the last which is only one more index that its character is 9 ...
29+ // so the final value ll be "99". then uses padEnd( 2, "0") that can not add "0" at the end, as we set the length "2".
30+ // 6. Lastly, we print the value of our pound and pence variable. which is "3.99".
0 commit comments