Skip to content

Commit 42fde43

Browse files
committed
3-to-pounds
1 parent 96f49e8 commit 42fde43

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Sprint-1/3-mandatory-interpret/3-to-pounds.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,22 @@ console.log(`£${pounds}.${pence}`);
2525

2626
// To begin, we can start with
2727
// 1. const penceString = "399p": initialises a string variable with the value "399p"
28+
29+
// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);
30+
// Removes the trailing "p" from the string, leaving just the numeric part (e.g., "399").
31+
32+
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
33+
// Pads the numeric string on the left with zeros so it is at least 3 characters long (e.g., "9" becomes "009").
34+
// This ensures consistent formatting for the next steps.
35+
36+
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);
37+
// Extracts all but the last two characters to represent the pounds part (e.g., from "399", it takes "3").
38+
39+
// 5. const pence = paddedPenceNumberString
40+
// .substring(paddedPenceNumberString.length - 2)
41+
// .padEnd(2, "0");
42+
// Extracts the last two characters for the pence part (e.g., from "399", it takes "99").
43+
// If there are fewer than two characters, it pads the result on the right with zeros.
44+
45+
// 6. console.log(`£${pounds}.${pence}`);
46+
// Outputs the formatted price in pounds and pence

0 commit comments

Comments
 (0)