Skip to content

Commit 49bec3d

Browse files
committed
3-to-pounds.js
1 parent e8b8bb9 commit 49bec3d

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,27 @@ 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+
/*
30+
31+
1. const penceString = "399p";
32+
Initializes a string variable with the value "399p", representing a price in pence with a trailing "p".
33+
34+
2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);
35+
Removes the trailing "p" character from the string, leaving only the numeric part (e.g., "399").
36+
37+
3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
38+
Pads the numeric string on the left with zeros so it is at least 3 characters long (e.g., "399" stays "399", but "9" would become "009"). This ensures consistent formatting for the next steps.
39+
40+
4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);
41+
Extracts all but the last two characters to represent the pounds part (e.g., from "399", it takes "3").
42+
43+
5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
44+
Extracts the last two characters for the pence part (e.g., from "399", it takes "99"). If there are fewer than two characters, it pads the result on the right with zeros.
45+
46+
c6. onsole.log(£${pounds}.${pence});
47+
Outputs the formatted price in pounds and pence (e.g., "£3.99").
48+
49+
50+
*/
51+

0 commit comments

Comments
 (0)