Skip to content

Commit ec4e15c

Browse files
authored
I have added a brief explanation, after each line of code, to rationalize the reasoning behind the code.
1 parent 33eb3b2 commit ec4e15c

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
const penceString = "399p";
2-
2+
// 1. const penceString = "399p": initialises a string variable with the value "399p"
33
const penceStringWithoutTrailingP = penceString.substring(
44
0,
55
penceString.length - 1
66
);
7-
7+
//2. Removes the trailing "p" from the pencestring, leaving 399 as the new value.
88
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
9+
//3. This line ensures there are three characters, at least, by adding "0" to the start if necessary.
910
const pounds = paddedPenceNumberString.substring(
1011
0,
1112
paddedPenceNumberString.length - 2
1213
);
13-
14+
//4. We are extracting the pounds now, ie the first character.
1415
const pence = paddedPenceNumberString
1516
.substring(paddedPenceNumberString.length - 2)
1617
.padEnd(2, "0");
17-
18+
//4. Here, we are extracting the last two digits, ie pence. Padend insures there are two digits and adds a "0" to compensate, if necessary.
1819
console.log(${pounds}.${pence}`);
20+
//5. Here it should show the final result of £3.99
21+
1922

2023
// This program takes a string representing a price in pence
2124
// The program then builds up a string representing the price in pounds
2225

2326
// You need to do a step-by-step breakdown of each line in this program
2427
// Try and describe the purpose / rationale behind each step
25-
26-
// To begin, we can start with
27-
// 1. const penceString = "399p": initialises a string variable with the value "399p"

0 commit comments

Comments
 (0)