Skip to content

Commit 0dc471c

Browse files
described the purpose and rationale behind each step
1 parent 4982dbf commit 0dc471c

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
const penceString = "399p";
22

3-
const penceStringWithoutTrailingP = penceString.substring(
4-
0,
5-
penceString.length - 1
6-
);
3+
const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);
74

85
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
96
const pounds = paddedPenceNumberString.substring(
@@ -25,3 +22,17 @@ console.log(`£${pounds}.${pence}`);
2522

2623
// To begin, we can start with
2724
// 1. const penceString = "399p": initialises a string variable with the value "399p"
25+
// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1); Here we use subString() to extracts everything except the last character.
26+
// 3.const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
27+
// padStart(3, "0") ensures the string has at least 3 characters.
28+
// If the string is shorter than 3, zeros ("0") are added at the beginning.
29+
// If the string is already 3 or more characters, nothing changes.
30+
// padStart() ensures the code works for all pence values, including 1-digit and 2-digit numbers.
31+
// Without padStart(), the logic would only work correctly for numbers that already have 3 or more digits.
32+
// 4.const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);
33+
// This line used extract the pound value 3 from the padded pence string 399 .
34+
// 5.const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
35+
//This line extracts the last two digits from paddedPenceNumberString by starting at length - 2 and going to the end.
36+
// Then padEnd(2, "0") ensures the pence value always has two digits.
37+
// 6.console.log(`£${pounds}.${pence}`);
38+
// Here we use console.log() to print our output and merge the pound value and pence value and add pound symbol using template literals.

0 commit comments

Comments
 (0)