11const penceString = "399p" ;
2-
32const penceStringWithoutTrailingP = penceString . substring (
43 0 ,
54 penceString . length - 1
65) ;
7-
86const paddedPenceNumberString = penceStringWithoutTrailingP . padStart ( 3 , "0" ) ;
97const pounds = paddedPenceNumberString . substring (
108 0 ,
119 paddedPenceNumberString . length - 2
1210) ;
13-
1411const pence = paddedPenceNumberString
1512 . substring ( paddedPenceNumberString . length - 2 )
1613 . padEnd ( 2 , "0" ) ;
17-
1814console . log ( `£${ pounds } .${ pence } ` ) ;
15+ // This program takes a string representing a price in pence.
16+ // The program then builds up a string representing the price in pounds.
17+
18+ // Step-by-step breakdown:
19+
20+ // 1. const penceString = "399p";
21+ // Initialises the penceString variable with the string "399p".
22+ // This represents a price of 399 pence.
23+
24+ // 2. penceString.length - 1
25+ // penceString has a length of 4. Subtracting 1 gives 3.
26+ // This is used to identify the position before the final "p".
27+
28+ // 3. penceString.substring(0, penceString.length - 1)
29+ // substring() extracts the characters from index 0 up to, but not including,
30+ // index 3. This removes the trailing "p" and produces the string "399".
31+
32+ // 4. const penceStringWithoutTrailingP = ...
33+ // Stores the result "399", so the price now contains only the numeric characters.
34+
35+ // 5. penceStringWithoutTrailingP.padStart(3, "0")
36+ // padStart() makes sure the string contains at least 3 characters.
37+ // If it has fewer than 3 characters, "0" is added to the beginning.
38+ // For "399", no padding is needed, so the value remains "399".
39+ // This is useful for smaller values such as "99", which would become "099".
40+
41+ // 6. const paddedPenceNumberString = ...
42+ // Stores the padded string. For the current input, its value is "399".
43+
44+ // 7. paddedPenceNumberString.length - 2
45+ // This calculates the position that separates the pounds from the final
46+ // two digits representing pence. For "399", the length is 3, so 3 - 2 = 1.
47+
48+ // 8. paddedPenceNumberString.substring(
49+ // 0,
50+ // paddedPenceNumberString.length - 2
51+ // )
52+ // Extracts the characters before the final two digits.
53+ // For "399", this extracts "3", which represents the pounds.
54+
55+ // 9. const pounds = ...
56+ // Stores the pounds part of the price. In this example, pounds is "3".
57+
58+ // 10. paddedPenceNumberString
59+ // .substring(paddedPenceNumberString.length - 2)
60+ // substring() starts two characters from the end of the string.
61+ // For "399", it extracts "99", which represents the pence part.
1962
20- // This program takes a string representing a price in pence
21- // The program then builds up a string representing the price in pounds
63+ // 11. .padEnd(2, "0")
64+ // Makes sure the pence part contains at least two characters.
65+ // If necessary, "0" is added to the end until the string has a length of 2.
66+ // In this example, "99" already has two characters, so it remains "99".
2267
23- // You need to do a step-by-step breakdown of each line in this program
24- // Try and describe the purpose / rationale behind each step
68+ // 12. const pence = ...
69+ // Stores the final pence part. In this example, pence is "99".
2570
26- // To begin, we can start with
27- // 1. const penceString = "399p": initialises a string variable with the value "399p"
71+ // 13. console.log(`£${pounds}.${pence}`);
72+ // Uses a template literal to combine the pound sign, pounds value,
73+ // decimal point and pence value.
74+ // With the input "399p", the final output is "£3.99".
0 commit comments