Skip to content

Commit 16d0805

Browse files
committed
Complete to-pounds interpret exercise with step breakdown
1 parent 4aa5b32 commit 16d0805

1 file changed

Lines changed: 36 additions & 2 deletions

File tree

‎Sprint-2/3-mandatory-interpret/3-to-pounds.js‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,39 @@ console.log(`£${pounds}.${pence}`);
2323
// You need to do a step-by-step breakdown of each line in this program
2424
// Try and describe the purpose / rationale behind each step
2525

26-
// To begin, we can start with
27-
// 1. const penceString = "399p": initialises a string variable with the value "399p"
26+
//Answer:
27+
28+
// 1. const penceString = "399p"
29+
// Initialises a string variable with the value "399p"
30+
// (price written in pence, with a trailing "p").
31+
32+
// 2. penceString.substring(0, penceString.length - 1)
33+
// Removes the final character "p".
34+
// For "399p" this becomes "399".
35+
// Stored in penceStringWithoutTrailingP.
36+
37+
// 3. penceStringWithoutTrailingP.padStart(3, "0")
38+
// Ensures the string is at least 3 characters long by padding
39+
// with "0" on the left if needed.
40+
// "399" stays "399".
41+
// Example: "5" would become "005", "42" would become "042".
42+
// This makes it easier to always treat the last 2 digits as pence.
43+
44+
// 4. paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2)
45+
// Takes everything except the last 2 characters → the pounds part.
46+
// For "399" this is "3".
47+
48+
// 5. paddedPenceNumberString.substring(paddedPenceNumberString.length - 2)
49+
// Takes only the last 2 characters → the pence part.
50+
// For "399" this is "99".
51+
52+
// 6. .padEnd(2, "0") on the pence part
53+
// Ensures pence is always 2 digits (adds "0" on the right if needed).
54+
// "99" stays "99".
55+
// Example: if pence were "5", it would become "50" with padEnd —
56+
// (note: for money, padStart is usually more common for the numeric part;
57+
// here the code uses padEnd as written).
58+
59+
// 7. console.log(`£${pounds}.${pence}`)
60+
// Prints the final price in pounds format.
61+
// For this input: £3.99

0 commit comments

Comments
 (0)