|
1 | | -const penceString = "399p"; |
2 | | - |
3 | | -const penceStringWithoutTrailingP = penceString.substring( |
4 | | - 0, |
5 | | - penceString.length - 1 |
6 | | -); |
7 | | - |
8 | | -const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); |
9 | | -const pounds = paddedPenceNumberString.substring( |
10 | | - 0, |
11 | | - paddedPenceNumberString.length - 2 |
12 | | -); |
13 | | - |
14 | | -const pence = paddedPenceNumberString |
15 | | - .substring(paddedPenceNumberString.length - 2) |
16 | | - .padEnd(2, "0"); |
17 | | - |
18 | | -console.log(`£${pounds}.${pence}`); |
19 | | - |
20 | | -// This program takes a string representing a price in pence |
21 | | -// The program then builds up a string representing the price in pounds |
22 | | - |
23 | | -// You need to do a step-by-step breakdown of each line in this program |
24 | | -// 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" |
| 1 | +const penceString = "399p"; // initialises a string variable with the value "399p" |
| 2 | + |
| 3 | +const penceStringWithoutTrailingP = penceString.substring( |
| 4 | + 0, |
| 5 | + penceString.length - 1 |
| 6 | +); // use .substring method to extract characters of numerical string, with zero indexing starting from first place of penceString, ending at one digit less than the length of penceString by method .length. Expecting value "399". |
| 7 | + |
| 8 | +const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); |
| 9 | +//declares paddedPenceNumberString variable. To target the length of pence number string in three characters. If not, "0" will be added at the beginning of the string. Expecting value "399". |
| 10 | +const pounds = paddedPenceNumberString.substring( |
| 11 | + 0, |
| 12 | + paddedPenceNumberString.length - 2 |
| 13 | +); //declares variable for pound. .substring method starting from first character, ending by trimming last two characters by method .length. Expecting value "3" |
| 14 | + |
| 15 | +const pence = paddedPenceNumberString |
| 16 | + .substring(paddedPenceNumberString.length - 2) |
| 17 | + .padEnd(2, "0"); // declares variable for pence. Argument -2 of .substring method returns the last two characters. |
| 18 | + // .padEnd method returns character length of two, if not, "0" will be added at the end of string, for examples "90". Here, expecting "99". |
| 19 | + |
| 20 | +console.log(`£${pounds}.${pence}`); // Prints the return value by Template Literal with '£X.yz" format in console pane. |
| 21 | + |
| 22 | + |
| 23 | +// This program takes a string representing a price in pence |
| 24 | +// The program then builds up a string representing the price in pounds |
| 25 | + |
| 26 | +// You need to do a step-by-step breakdown of each line in this program |
| 27 | +// Try and describe the purpose / rationale behind each step |
| 28 | + |
| 29 | +// To begin, we can start with |
| 30 | +// 1. const penceString = "399p": initialises a string variable with the value "399p" |
| 31 | + |
| 32 | +// Five Variables declared: |
| 33 | +// penceString, penceStringWithoutTrailingP, paddedPenceNumberString,pound, pence |
| 34 | +// Three methods used: |
| 35 | +//.substring(), .padStart(), .padEnd() .log() |
| 36 | +// One property used: .length |
0 commit comments