|
| 1 | +let carPrice = "10,000"; |
| 2 | +let priceAfterOneYear = "8,543"; |
| 3 | + |
| 4 | +carPrice = Number(carPrice.replaceAll(",", "")); |
| 5 | +priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); |
| 6 | + |
| 7 | +const priceDifference = carPrice - priceAfterOneYear; |
| 8 | +const percentageChange = (priceDifference / carPrice) * 100; |
| 9 | + |
| 10 | +console.log(`The percentage change is ${percentageChange}`); |
| 11 | + |
| 12 | +// Read the code and then answer the questions below |
| 13 | + |
| 14 | +// a) How many function calls are there in this file? Write down all the lines where a function call is made |
| 15 | +// Line 4 calls the replaceAll() function and the Number() function |
| 16 | +// Line 5 calls the replaceAll() function and the Number() function |
| 17 | +// Line 10 calls the console.log() function |
| 18 | +// 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? |
| 19 | +// priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); |
| 20 | +// ^^^ |
| 21 | + |
| 22 | +// SyntaxError: missing ) after argument list |
| 23 | +// The error is occurring because there is a missing comma in the replaceAll() function |
| 24 | +// The replaceAll() function should be replaceAll(",", "") like the one on line 4 |
| 25 | +// I've fixed it by adding the comma |
| 26 | + |
| 27 | +// c) Identify all the lines that are variable reassignment statements |
| 28 | +// Line 4 and Line 5 are variable reassignment statements. The variables carPrice and priceAfterOneYear are being reassigned to new values |
| 29 | + |
| 30 | +// d) Identify all the lines that are variable declarations |
| 31 | +// 1,2,7,8 are variable declarations. The variables carPrice, priceAfterOneYear, priceDifference, and percentageChange are being declared |
| 32 | + |
| 33 | +// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? |
| 34 | + |
| 35 | +// working from the inside out: |
| 36 | +// the replaceAll() function is removing the comma from the string stored in the carPrice variable |
| 37 | +// the Number() function is converting the string to a number |
0 commit comments