Skip to content

Commit 3adb077

Browse files
committed
Changed the comments and implementation making it more cleaner and shorter.
1 parent bec2478 commit 3adb077

File tree

1 file changed

+8
-54
lines changed

1 file changed

+8
-54
lines changed

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

Lines changed: 8 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@ 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"
28-
26+
// To begin we start with
2927
// const penceString = "399p";
28+
//
3029
// Initialises a string variable with the value "399p".
31-
// Example value now: "399p"
30+
// penceString variable now store the value: "399p"
3231

3332
// const penceStringWithoutTrailingP = penceString.substring(
3433
// 0,
@@ -37,82 +36,37 @@ console.log(`£${pounds}.${pence}`);
3736

3837

3938
// What it does: takes a substring of penceString from index 0 up to (but not including) penceString.length - 1.
40-
// Why: removes the last character (the trailing "p") so we are left with just the numeric part.
41-
// Example: "399p".substring(0, 4 - 1) → "399".
42-
// Result stored: penceStringWithoutTrailingP === "399"
39+
// And removes the last character (the trailing "p") so we are left with just the numeric part.
4340

4441
// const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
4542

46-
47-
// What it does: ensures the numeric string has at least 3 characters by adding leading "0" characters if necessary.
48-
// Why: the subsequent logic expects at least three digits so we can safely slice off "pounds" (all but the last two digits) and "pence" (last two digits). Padding handles small values like "5p" → ensures correct indexing.
49-
// Example: "399".padStart(3, "0") → "399" (no change). If input had been "5", .padStart(3,"0") → "005".
43+
// What it does: ensures the numeric string has at least 3 characters by adding leading "0" characters.
44+
// The subsequent logic expects at least three digits so we can safely slice off "pounds" (all except the last two digits) and "pence" (last two digits). After that padding can handles small values like "5p" while maintaing correct indexing.
5045
// Result stored: paddedPenceNumberString === "399"
5146

5247
// const pounds = paddedPenceNumberString.substring(
5348
// 0,
5449
// paddedPenceNumberString.length - 2
5550
// );
5651

57-
5852
// What it does: takes the substring from index 0 up to (but not including) the last two characters. Those leading characters represent whole pounds.
59-
// Why: British currency: last two digits are pence, the digits before them are pounds. This extracts the pounds portion.
60-
// Example: "399".substring(0, 3 - 2) → "3". So pounds === "3", meaning £3.
6153
// Note: Because of padStart(3, "0"), this always returns at least one character (for values < 100 it returns "0" or more).
6254

6355
// const pence = paddedPenceNumberString
6456
// .substring(paddedPenceNumberString.length - 2)
6557
// .padEnd(2, "0");
6658

67-
6859
// What it does (first part): .substring(length - 2) returns the final two characters (the pence digits).
6960
// What it does (second part): .padEnd(2, "0") ensures the result has at least two characters by adding trailing zeros if needed.
70-
// Why: extracting the two pence digits is necessary to format £x.yy. The padEnd is defensive: if the substring somehow produced a single character (it shouldn't after the earlier padStart, but this makes the code more robust), it will ensure two digits (e.g., "5" → "50").
7161
// Example: "399".substring(1) → "99". .padEnd(2,"0") → still "99". So pence === "99".
7262

7363
// console.log(`£${pounds}.${pence}`);
7464

75-
76-
// What it does: prints a formatted pounds-and-pence string to the console, using template interpolation.
77-
// Why: final user-facing representation of the price.
65+
// What it does: prints a formatted pounds-and-pence string and symbol to the console, using template interpolation.
66+
// Why: final result showing what user would expect the representation of the price.
7867
// Example output: £3.99
7968

80-
// Quick summary of the data flow (with the example "399p")
81-
82-
// penceString → "399p"
83-
84-
// remove trailing p → "399" (penceStringWithoutTrailingP)
85-
86-
// pad to at least 3 chars → "399" (paddedPenceNumberString)
87-
88-
// pounds = all but last two chars → "3"
89-
90-
// pence = last two chars → "99"
91-
92-
// printed string → £3.99
93-
94-
// Why some steps (like padStart / padEnd) are needed
95-
96-
// padStart(3, "0") ensures inputs shorter than 3 digits (e.g. "5p" or "45p") still produce correct pound/pence splitting:
97-
98-
// "5p" → "5" → pad → "005" → pounds = "0", pence = "05" → £0.05
99-
100-
// "45p" → "45" → pad → "045" → pounds = "0", pence = "45" → £0.45
101-
102-
// padEnd(2, "0") is defensive to guarantee two pence digits (useful if some earlier step produced one digit).
103-
104-
// Edge cases & small improvements
105-
106-
// If the input lacks the trailing "p" or contains non-digits ("£3.99", "abc") the code will produce incorrect results or unexpected output. Consider validating the string first (e.g., /^\d+p$/).
107-
108-
// For clarity and robustness you could parse the number then compute pounds/pence numerically:
109-
11069
let numericPence = parseInt(penceString.replace(/\D/g, ""), 10);
11170
pounds = Math.floor(numericPence / 100);
11271
pence = String(numericPence % 100).padStart(2, "0");
11372

114-
115-
// This avoids fiddly substring indices and better expresses intent.
116-
117-
// padEnd(2, "0") after slicing the last two characters is usually unnecessary if padStart(3,"0") is present — but leaving it is harmless and defensive.
118-

0 commit comments

Comments
 (0)