Skip to content

Commit e82c680

Browse files
committed
Update penceString value and enhance console logging for clarity in 3-to-pounds.js
1 parent 711385f commit e82c680

File tree

1 file changed

+54
-12
lines changed

1 file changed

+54
-12
lines changed
Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,69 @@
1-
const penceString = "399p";
1+
const penceString = "3679p";
22

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

87
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
9-
const pounds = paddedPenceNumberString.substring(
10-
0,
11-
paddedPenceNumberString.length - 2
12-
);
138

14-
const pence = paddedPenceNumberString
15-
.substring(paddedPenceNumberString.length - 2)
16-
.padEnd(2, "0");
9+
console.log(paddedPenceNumberString);
10+
11+
const pounds = paddedPenceNumberString.substring( 0, paddedPenceNumberString.length - 2);
12+
13+
console.log(pounds);
14+
15+
const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
16+
17+
console.log(pence);
1718

1819
console.log(${pounds}.${pence}`);
1920

21+
22+
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
27+
28+
29+
2430
// Try and describe the purpose / rationale behind each step
2531

2632
// To begin, we can start with
2733
// 1. const penceString = "399p": initialises a string variable with the value "399p"
34+
35+
/*1. const penceString = "399p";
36+
37+
We create a variable called penceString and give it the value "399p".
38+
39+
This is the price in pence, written as text.
40+
41+
2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);
42+
43+
We remove the last letter "p" from the string.
44+
45+
Now we only have the numbers, for example "399".
46+
47+
**3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
48+
49+
We make sure the string has at least 3 characters by adding zeros at the start if needed.
50+
51+
Example: "50" becomes "050" and "5" becomes "005".
52+
53+
4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);
54+
55+
We take all characters except the last two.
56+
57+
These characters are the pounds. Example: "399" → "3" pounds.
58+
59+
5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
60+
61+
We take the last two characters. These are the pence.
62+
63+
We add a zero at the end if needed to always have two digits. Example: "5" → "05".
64+
65+
6. console.log(£${pounds}.${pence});
66+
67+
We show the price in pounds and pence.
68+
69+
Example: "399p" → £3.99, "50p" → £0.50. */

0 commit comments

Comments
 (0)