Skip to content

Commit 60283cb

Browse files
committed
Convert pence to pounds
1 parent f33a665 commit 60283cb

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,20 @@ 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+
1. const penceString = "399p" // Stores "399p" in a variable called penceString so the program can work with the price.
30+
2. const penceStringWithoutTrailingP = penceString.substring(
31+
0,
32+
penceString.length - 1
33+
); // Removes the "p" from the end of "399p", leaving "399"
34+
3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
35+
//Adds a "0" at the start if there are less than 3 digits.
36+
4. const pounds = paddedPenceNumberString.substring(
37+
0,
38+
paddedPenceNumberString.length - 2); //gets the digits before the last two to find the pounds.
39+
40+
5. const pence = paddedPenceNumberString
41+
.substring(paddedPenceNumberString.length - 2)
42+
.padEnd(2, "0"); //gets the last two digits to find the pence.
43+
44+
6. console.log(`£${pounds}.${pence}`); //Combines the pounds and pence and print the final price as £3.99

0 commit comments

Comments
 (0)