Skip to content

Commit b50a6c4

Browse files
authored
Update 3-to-pounds.js
Add detailed comments explaining step-by-step price formatting from pence string.
1 parent b276dfb commit b50a6c4

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
11
const penceString = "399p";
2+
// 1. Initializes a string variable with the value "399p" representing the price in pence with a trailing 'p'.
23

34
const penceStringWithoutTrailingP = penceString.substring(
45
0,
56
penceString.length - 1
67
);
8+
// 2. Removes the trailing 'p' by taking a substring from index 0 up to (but not including) the last character.
9+
// Result: "399"
710

811
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
12+
// 3. Ensures the string is at least 3 characters long by padding with leading zeros if needed.
13+
// In this case, "399" is already 3 characters, so it remains "399".
14+
915
const pounds = paddedPenceNumberString.substring(
1016
0,
1117
paddedPenceNumberString.length - 2
1218
);
19+
// 4. Extracts the pounds part by taking all characters except the last two.
20+
// For "399", this is "3" (the hundreds place).
1321

1422
const pence = paddedPenceNumberString
1523
.substring(paddedPenceNumberString.length - 2)
1624
.padEnd(2, "0");
25+
// 5. Extracts the last two characters as the pence part.
26+
// For "399", this is "99".
27+
// Then pads the string on the right with zeros if it’s shorter than 2 characters (not needed here).
1728

1829
console.log(${pounds}.${pence}`);
19-
20-
// This program takes a string representing a price in pence
21-
// The program then builds up a string representing the price in pounds
22-
23-
// You need to do a step-by-step breakdown of each line in this program
24-
// Try and describe the purpose / rationale behind each step
25-
26-
// To begin, we can start with
27-
// 1. const penceString = "399p": initialises a string variable with the value "399p"
30+
// 6. Prints the formatted price string with a £ sign, pounds, and pence separated by a dot.
31+
// Output: "£3.99"

0 commit comments

Comments
 (0)