Skip to content

Commit 544f99d

Browse files
explained the steps of the programs output
1 parent 2f89dbe commit 544f99d

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1+
//declaring a variable with the value of "399p"
12
const penceString = "399p";
23

4+
//the .substring is removing the "p" from the value of penceString by taking the total length of the subString "4" - 1 = 3, by stripping the "p" leaving only 399
35
const penceStringWithoutTrailingP = penceString.substring(
46
0,
57
penceString.length - 1
68
);
79

10+
// padStart is creating a pad for the output by giving it an extra "0" if the value is less than £1
811
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
12+
13+
//The .substring is removing the "99" from the value of .penceStringWithoutTrailingP by taking the total length of the .penceStringWithoutTrailingP "3" - 2 = 1 by stripping the "99" leaving only the 3
914
const pounds = paddedPenceNumberString.substring(
1015
0,
1116
paddedPenceNumberString.length - 2
1217
);
1318

19+
// the .substring is doing the same as the "pound" variable declared in now line 14, by grabbing the last 2 characters "99" except it is ending the pad as safeguard to always ensure that two digits remain
1420
const pence = paddedPenceNumberString
1521
.substring(paddedPenceNumberString.length - 2)
1622
.padEnd(2, "0");
1723

24+
// console.log is using template literals to combine the currency symbol, pound string decimal point and pence string. Giving us an output of £3.99
1825
console.log(`£${pounds}.${pence}`);
1926

2027
// This program takes a string representing a price in pence

0 commit comments

Comments
 (0)