@@ -113,4 +113,45 @@ A better name could be:
113113### f) Will this work for all values of movieLength?
114114Yes, it works for any positive number of seconds.
115115However, if movieLength is negative, a decimal, or not a number, the result would be incorrect.
116- Also, minutes and seconds are not padded with leading zeros, so values like 2:3:5 may appear instead of 02:03:05.
116+ Also, minutes and seconds are not padded with leading zeros, so values like 2:3:5 may appear instead of 02:03:05.
117+
118+
119+
120+
121+ ## 3-to-pounds.js
122+
123+ ### Step-by-step breakdown (line by line)
124+
125+ 1 ) ` const penceString = "399p"; `
126+ - Declares a string containing a price in pence, with a trailing ` p ` character.
127+
128+ 2 ) ` const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1); `
129+ - Takes a substring from index 0 up to (but not including) the last character.
130+ - Purpose: remove the trailing ` "p" ` .
131+ - For ` "399p" ` , this becomes ` "399" ` .
132+
133+ 3 ) ` const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); `
134+ - Pads the string on the left until it is at least length 3, using ` "0" ` .
135+ - Purpose: make sure we always have enough digits to represent pounds + pence.
136+ - Example: ` "5" ` becomes ` "005" ` , ` "50" ` becomes ` "050" ` , ` "399" ` stays ` "399" ` .
137+
138+ 4 ) ` const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); `
139+ - Takes everything except the last 2 digits.
140+ - Purpose: the part before the last 2 digits is the pounds.
141+ - For ` "399" ` , length is 3, so substring(0, 1) → ` "3" ` .
142+
143+ 5 ) ` const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); `
144+ - First, ` substring(length - 2) ` takes the last 2 digits.
145+ - Then ` padEnd(2, "0") ` ensures it’s at least 2 characters (adds zeros on the right if needed).
146+ - Purpose: get exactly two pence digits.
147+ - For ` "399" ` , last two digits are ` "99" ` → stays ` "99" ` .
148+
149+ 6 ) ` console.log(\ ` £${pounds}.${pence}\` );`
150+ - Uses a template literal to format the final currency string as pounds and pence.
151+ - For pounds ` "3" ` and pence ` "99" ` → outputs ` £3.99 ` .
152+
153+ ### Unfamiliar syntax (docs)
154+ - substring: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring
155+ - padStart: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
156+ - padEnd: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd
157+ - template literals: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
0 commit comments