|
1 | 1 | let carPrice = "10,000"; |
2 | 2 | let priceAfterOneYear = "8,543"; |
3 | 3 |
|
4 | | -carPrice = Number(carPrice.replaceAll(",", "")); |
5 | | -priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); |
| 4 | +carPrice = Number(carPrice.replaceAll(",", "")); // returns 10000 |
| 5 | +priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",",""));//returns 8543 |
| 6 | +//priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); |
| 7 | +// incorrect code missing syntax ie a comma |
6 | 8 |
|
7 | 9 | const priceDifference = carPrice - priceAfterOneYear; |
8 | 10 | const percentageChange = (priceDifference / carPrice) * 100; |
9 | 11 |
|
10 | | -console.log(`The percentage change is ${percentageChange}`); |
| 12 | +console.log(`The percentage change is ${percentageChange}`); //returns The percentage change is 14.57 |
11 | 13 |
|
12 | 14 | // Read the code and then answer the questions below |
13 | 15 |
|
14 | | -// a) How many function calls are there in this file? Write down all the lines where a function call is made |
| 16 | +// a) How many function calls are there in this file? |
| 17 | +// Write down all the lines where a function call is made |
| 18 | +//*Answer |
| 19 | +//There are 5 function calls: 2* Number(), 2*.replaceAll() and 1* console.log(). |
| 20 | +//carPrice = Number(carPrice.replaceAll(",", "")); |
| 21 | +//priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",","")); |
| 22 | +//console.log(`The percentage change is ${percentageChange}`); |
15 | 23 |
|
16 | | -// b) Run the code and identify the line where the error is coming from - why is this error occurring? How can you fix this problem? |
| 24 | +// b) Run the code and identify the line where the error is coming from - |
| 25 | +// why is this error occurring? How can you fix this problem? |
| 26 | +//*Answer it is missing a comma on line 5. |
| 27 | +// Add the comma to fix it: ...replaceAll(",","")); |
17 | 28 |
|
18 | 29 | // c) Identify all the lines that are variable reassignment statements |
| 30 | +//*Answer |
| 31 | +//carPrice = Number(carPrice.replaceAll(",", "")); |
| 32 | +//priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); |
| 33 | + |
19 | 34 |
|
20 | 35 | // d) Identify all the lines that are variable declarations |
| 36 | +//let carPrice = "10,000"; |
| 37 | +//let priceAfterOneYear = "8,543"; |
| 38 | +//const priceDifference = carPrice - priceAfterOneYear; |
| 39 | +//const percentageChange = (priceDifference / carPrice) * 100; |
| 40 | + |
| 41 | + |
| 42 | +// e) Describe what the expression |
| 43 | +// Number(carPrice.replaceAll(",","")) is doing - |
| 44 | +// what is the purpose of this expression? |
| 45 | +//It converts the carPrice into a number and removes the spaces and commas. |
| 46 | + |
21 | 47 |
|
22 | | -// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? |
|
0 commit comments