Skip to content

Commit 3f4ddcc

Browse files
committed
Explain step-by-step conversion from pence to pounds
1 parent 4f3358b commit 3f4ddcc

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,56 @@ 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+
// creates a string that represents a price in pence, including the letter "p"
30+
//
31+
// Example: "399p"
32+
//
33+
// This is the original input value we want to convert into pounds.
34+
35+
36+
// 2. const penceStringWithoutTrailingP = ...
37+
// Uses substring to remove the last character ("p") from the string
38+
//
39+
// We take the string from index 0 up to (but not including) the last character
40+
//
41+
// Result: "399"
42+
//
43+
// This leaves us with only the numeric part of the price as a string.
44+
45+
46+
// 3. const paddedPenceNumberString = ...
47+
// padStart ensures the string is at least 3 characters long
48+
//
49+
// This is important for small values like "5p":
50+
// "5" becomes "005"
51+
//
52+
// Result for "399": "399"
53+
// Result for "5p": "005"
54+
55+
56+
// 4. const pounds = ...
57+
// Extracts all characters except the last two
58+
//
59+
// The last two characters represent pence,
60+
// everything before that represents pounds
61+
//
62+
// For "399":
63+
// pounds = "3"
64+
65+
66+
// 5. const pence = ...
67+
// Takes the last two characters of the string
68+
//
69+
// padEnd ensures the pence value is always two digits
70+
//
71+
// For "399":
72+
// pence = "99"
73+
74+
75+
// 6. console.log(`£${pounds}.${pence}`)
76+
// Combines pounds and pence into a formatted price string
77+
//
78+
// Final output:
79+
// £3.99
80+

0 commit comments

Comments
 (0)