|
4 | 4 | // You will need to declare a function called toPounds with an appropriately named parameter. |
5 | 5 |
|
6 | 6 | // You should call this function a number of times to check it works for different inputs |
7 | | - |
8 | | -//============================================================= |
9 | | -// const penceString = "9p"; //init variable |
10 | | - |
11 | | -// const penceStringWithoutTrailingP = penceString.substring( |
12 | | -// 0, |
13 | | -// penceString.length - 1 |
14 | | -// ); //remove p character at the end of the variable |
15 | | -// console.log(penceStringWithoutTrailingP); |
16 | | - |
17 | | -// const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); // |
18 | | -// console.log(paddedPenceNumberString); //give format of 3 zeroes if the number is less than 3 digits. |
19 | | - |
20 | | -// const pounds = paddedPenceNumberString.substring( |
21 | | -// 0, |
22 | | -// paddedPenceNumberString.length - 2 |
23 | | -// ); //get ponds value conversion changes for given pence |
24 | | - |
25 | | -// console.log(pounds); |
26 | | - |
27 | | -// const pence = paddedPenceNumberString |
28 | | -// .substring(paddedPenceNumberString.length - 2) |
29 | | -// .padEnd(2, "0"); //get the reminds after conversion to pounds |
30 | | - |
31 | | -// console.log(`£${pounds}.${pence}`); |
32 | | - |
33 | | -//------------------------------------------------------------------------------------------ |
34 | | - |
35 | | -function pensToPounds(penceString) { |
36 | | - const penceStringWithoutTrailingP = penceString.substring( |
37 | | - 0, |
38 | | - penceString.length - 1 |
39 | | - ); //remove p character at the end of the variable |
40 | | - //console.log(penceStringWithoutTrailingP); |
41 | | - |
42 | | - const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); // |
43 | | - //console.log(paddedPenceNumberString); //give format of 3 zeroes if the number is less than 3 digits. |
44 | | - |
45 | | - const pounds = paddedPenceNumberString.substring( |
46 | | - 0, |
47 | | - paddedPenceNumberString.length - 2 |
48 | | - ); //get ponds value conversion changes for given pence |
49 | | - |
50 | | - //console.log(pounds); |
51 | | - |
52 | | - const pence = paddedPenceNumberString |
53 | | - .substring(paddedPenceNumberString.length - 2) |
54 | | - .padEnd(2, "0"); //get the reminds after conversion to pounds |
55 | | - |
56 | | - return `£${pounds}.${pence}`; |
57 | | -} |
58 | | - |
59 | | -//test----1 |
60 | | -let moneyA = "90p"; |
61 | | -console.log(`${pensToPounds(moneyA)}`); |
62 | | - |
63 | | -//test----2 |
64 | | -moneyA = "710p"; |
65 | | -console.log(`${pensToPounds(moneyA)}`); |
66 | | - |
67 | | -//test----3 |
68 | | -moneyA = "1210p"; |
69 | | -console.log(`${pensToPounds(moneyA)}`); |
0 commit comments