@@ -6,6 +6,7 @@ const penceStringWithoutTrailingP = penceString.substring(
66) ;
77
88const paddedPenceNumberString = penceStringWithoutTrailingP . padStart ( 3 , "0" ) ;
9+
910const pounds = paddedPenceNumberString . substring (
1011 0 ,
1112 paddedPenceNumberString . length - 2
@@ -14,6 +15,7 @@ const pounds = paddedPenceNumberString.substring(
1415const pence = paddedPenceNumberString
1516 . substring ( paddedPenceNumberString . length - 2 )
1617 . padEnd ( 2 , "0" ) ;
18+ console . log ( pence )
1719
1820console . log ( `£${ pounds } .${ pence } ` ) ;
1921
@@ -23,5 +25,35 @@ console.log(`£${pounds}.${pence}`);
2325// You need to do a step-by-step breakdown of each line in this program
2426// Try and describe the purpose / rationale behind each step
2527
26- // To begin, we can start with
27- // 1. const penceString = "399p": initialises a string variable with the value "399p"
28+ /*
29+ To begin, we can start with
30+ 1. const penceString = "399p": initialize a string variable with the value "399p"
31+
32+ 2. const penceStringWithoutTrailingP = penceString.substring(0,penceString.length - 1);
33+ ----- This is removing the last character from the string and because is negative argument
34+ we should provide the starter point and end index to override the character.
35+
36+ 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
37+ ----- The padStart function modifies the string by adding a specified character at the
38+ beginning if the string is shorter than the desired length. In this case,
39+ padStart(3, "0") checks if the string has fewer than 3 characters. If it does,
40+ it adds "0" at the beginning until the string reaches a length of 3. eg.
41+ const penceString = "9p"; => £0.09
42+ const penceString = "44p"; => £0.44
43+ const penceString = "0p"; => £0.00
44+
45+ 4. const pounds = paddedPenceNumberString.substring(0,paddedPenceNumberString.length - 2);
46+ ----- Here we are using again subtracting function to extract the integer number removing
47+ the decimals given the argument(0, string.length -2). this means return the string
48+ starting from position 0 and removing the last two characters.
49+
50+ 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
51+ ----- Extracting the decimal numbers and gets the last two characters of the string, which
52+ represent the pence part.
53+
54+ 6. console.log(`£${pounds}.${pence}`);
55+ ----- This line displays the formatted currency value by using template literals to interpolate
56+ the pounds and pence variables into a single string. The £ symbol is added at the beginning
57+ to indicate currency. eg.
58+ const penceString = "399p"; => £3.99
59+ */
0 commit comments