|
1 | | -const penceString = "399p"; |
| 1 | +const penceString = "3679p"; |
2 | 2 |
|
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); |
7 | 6 |
|
8 | 7 | const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); |
9 | | -const pounds = paddedPenceNumberString.substring( |
10 | | - 0, |
11 | | - paddedPenceNumberString.length - 2 |
12 | | -); |
13 | 8 |
|
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); |
17 | 18 |
|
18 | 19 | console.log(`£${pounds}.${pence}`); |
19 | 20 |
|
| 21 | + |
| 22 | + |
20 | 23 | // This program takes a string representing a price in pence |
21 | 24 | // The program then builds up a string representing the price in pounds |
22 | 25 |
|
23 | 26 | // You need to do a step-by-step breakdown of each line in this program |
| 27 | + |
| 28 | + |
| 29 | + |
24 | 30 | // Try and describe the purpose / rationale behind each step |
25 | 31 |
|
26 | 32 | // To begin, we can start with |
27 | 33 | // 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