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
// This program takes a string representing a price in pence
20
+
// This program takes a string representindng a price in pence
21
21
// The program then builds up a string representing the price in pounds
22
22
23
23
// You need to do a step-by-step breakdown of each line in this program
24
24
// Try and describe the purpose / rationale behind each step
25
25
26
26
// To begin, we can start with
27
27
// 1. const penceString = "399p": initialises a string variable with the value "399p"
28
+
// const penceString = "399p": initialises a string variable with the value "399p"
29
+
// 1. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1): creates a new string variable that removes the last character "p" from the original string
30
+
// 2. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): creates a new string variable that pads the original string with leading zeros to make it 3 characters long
31
+
// 3. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): extracts the pounds portion of the string by taking all characters except the last two
32
+
// 4. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): extracts the pence portion of the string by taking the last two characters and pads it with trailing zeros to make it 2 characters long
33
+
// 5. console.log(`£${pounds}.${pence}`): outputs the final formatted price in pounds and pence to the console
0 commit comments