|
1 | 1 | // In Sprint-1, there is a program written in interpret/to-pounds.js |
2 | 2 |
|
3 | 3 | // You will need to take this code and turn it into a reusable block of code. |
| 4 | + |
| 5 | +// need to get car price = let fullPrice |
| 6 | +//need get priceOneyear= let priceOneyear |
| 7 | +// need take the spaces off the both variables |
| 8 | +//need check the diference of the prices |
| 9 | +//multiplay the result for 100 to check the percentage |
| 10 | +// return the result with template literals |
| 11 | + |
| 12 | +//function needs to be valid with diferents inputs |
| 13 | + |
4 | 14 | // You will need to declare a function called toPounds with an appropriately named parameter. |
5 | 15 |
|
6 | 16 | // You should call this function a number of times to check it works for different inputs |
| 17 | + |
| 18 | + |
| 19 | +// let carPrice = "10,000"; |
| 20 | +// let priceAfterOneYear = "8,543"; |
| 21 | + |
| 22 | +// carPrice = Number(carPrice.replaceAll(",", ""));// 10000 |
| 23 | +// priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",","")); //8543 |
| 24 | + |
| 25 | +// const priceDifference = carPrice - priceAfterOneYear; //1457 |
| 26 | +// const percentageChange = (priceDifference / carPrice) * 100; //145700 |
| 27 | + |
| 28 | +// console.log(`The percentage change is ${percentageChange}`); //14.57 |
| 29 | + |
| 30 | +function toPounds(fullPrice, priceOneyear) { |
| 31 | + |
| 32 | + fullPrice = Number(fullPrice.replaceAll(",", "")); |
| 33 | + priceOneyear = Number(priceOneyear.replaceAll(",", "")); |
| 34 | + |
| 35 | + const priceDifference = fullPrice - priceOneyear; |
| 36 | + |
| 37 | + const percentageChange = (priceDifference / fullPrice) * 100; |
| 38 | + |
| 39 | +// console.log(`The percentage change is ${percentageChange}`); |
| 40 | + return percentageChange; |
| 41 | +} |
| 42 | + |
| 43 | +console.log (toPounds( "100,000", "98,000")); |
0 commit comments