|
6 | 6 | // You should call this function a number of times to check it works for different inputs |
7 | 7 |
|
8 | 8 | function toPounds(penceString) { |
9 | | - // Remove trailing 'p' and pad with zeros to ensure at least 3 digits |
10 | | - const pence = penceString.substring(0, penceString.length - 1).padStart(3, "0"); |
11 | | - // Get pounds and pence parts |
12 | | - const pounds = pence.substring(0, pence.length - 2); |
13 | | - const pencePart = pence.substring(pence.length - 2).padEnd(2, "0"); |
14 | | - // Return formatted string |
15 | | - return `£${pounds}.${pencePart}`; |
| 9 | + // Remove trailing 'p' and pad with zeros to ensure at least 3 digits |
| 10 | + const pence = penceString |
| 11 | + .substring(0, penceString.length - 1) |
| 12 | + .padStart(3, "0"); |
| 13 | + // Get pounds and pence parts |
| 14 | + const pounds = pence.substring(0, pence.length - 2); |
| 15 | + const pencePart = pence.substring(pence.length - 2).padEnd(2, "0"); |
| 16 | + // Return formatted string |
| 17 | + return `£${pounds}.${pencePart}`; |
16 | 18 | } |
17 | 19 |
|
18 | | - |
19 | | -console.log(toPounds("399p")); // £3.99 |
20 | | -console.log(toPounds("9p")); // £0.09 |
21 | | -console.log(toPounds("99p")); // £0.99 |
22 | | -console.log(toPounds("100p")); // £1.00 |
23 | | -console.log(toPounds("1234p")); // £12.34 |
| 20 | +console.log(toPounds("399p")); // £3.99 |
| 21 | +console.log(toPounds("9p")); // £0.09 |
| 22 | +console.log(toPounds("99p")); // £0.99 |
| 23 | +console.log(toPounds("100p")); // £1.00 |
| 24 | +console.log(toPounds("1234p")); // £12.34 |
0 commit comments