Skip to content

Commit 6c9a6fa

Browse files
author
Ogbemi mene
committed
step by step breakdown
1 parent c0e0589 commit 6c9a6fa

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,32 @@ console.log(`£${pounds}.${pence}`);
2424
// Try and describe the purpose / rationale behind each step
2525

2626
// To begin, we can start with
27-
// 1. const penceString = "399p": initialises a string variable with the value "399p"
27+
// const penceString = "399p": initialises a string variable with the value "399p"
28+
// Rationale: This serves as the raw input data for the program,
29+
// simulating a price format you might get from a user input or a database.
30+
31+
// const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);
32+
// // What it does: Extracts a portion of penceString starting from index 0 up to (but not including) the very last character.
33+
// It stores "399" in penceStringWithoutTrailingP.
34+
// Rationale: To do any math or structural formatting,
35+
// the trailing "p" needs to be stripped away so the program is left with just the numeric characters.
36+
37+
// const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
38+
// What it does: Pads the start of the string with "0"s until the string reaches a total length of 3 characters.
39+
// For "399", it stays "399". However, if the input was "5p", this step would turn "5" into "005".
40+
// Rationale: This is a safety measure for small amounts (under £1.00).
41+
// By forcing a minimum length of 3 characters, it ensures there are always enough digits to separate into at least one digit for pounds and two digits for pence.
42+
43+
// const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);
44+
// What it does: Grabs everything from the start of the padded string up to the final 2 characters.
45+
// For "399", it extracts "3". For a padded string like "005", it would extract "0".
46+
// Rationale: Since 100 pence equals 1 pound, the last two digits of any pence value will always represent the change, and everything before those last two digits represents the whole pounds.
47+
48+
// const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
49+
// What it does: This does two things. First, .substring(...) extracts exactly the last 2 characters of the string (e.g., "99" from "399").
50+
// Then, .padEnd(2, "0") ensures it is 2 characters long by adding zeros to the right (though because of the previous steps, it's already 2 characters long here).
51+
// Rationale: This isolates the final two digits to represent the pence column cleanly, ensuring it always displays as a standard two-digit currency format (like .05 or .99).
52+
53+
// console.log(\£${pounds}.${pence}`);`
54+
// What it does: Uses a template literal to combine the pound sign (£), the pounds variable, a decimal point (.), and the pence variable into one string, printing £3.99 to the console.
55+
// Rationale: This is the final presentation layer, formatting the separated data pieces into a human-readable UK currency format.

0 commit comments

Comments
 (0)