You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Try and describe the purpose / rationale behind each step
25
25
26
26
// To begin, we can start with
27
-
// 1. const penceString = "399p": initialises a string variable with the value "399p" representing the price in pence including the trailing letter 'p'
28
-
// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1): Extract the numeric portion of the penceString by taking characters from index 0 up to the last character removing the 'p'
29
-
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP . padStart(3, "0"): it ensures the penceString is at least 3 digits long by adding leading zeros if necessary
27
+
// 1. const penceString = "399p": initializes a string variable with the value "399p" representing the price in pence including the trailing letter 'p'
28
+
29
+
// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1): Extract the numeric portion of the penceString by taking characters from index 0 up to the last character removing the 'p'because 399p is not a valid number for math.
30
+
31
+
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP . padStart(3, "0"): it ensures the penceString is at least 3 digits long by adding leading zeros if necessary ensuring there will always be 2 digits for pence and 1 for pound.
32
+
30
33
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): Extracts the pounds portion of the price by taking all digits except the last 2
34
+
31
35
// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2) padEnd(2, "0") it extracts the last 2 digits representing pence ensuring it is padded to 2 digits
32
-
// 6. line 18 = formats and prints the final price string with the pound symbol, amount, period and pence amount
36
+
37
+
// 6. line 18 = formats and prints the final price string with the pound symbol, amount, period and pence amount i.e "£3.99"
0 commit comments