|
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 | | -// There are 5 function calls. In the fourth line Number () and replaceAll (). |
16 | | -// In the fifth line same as line 4 Number () and replaceAll (). |
17 | | -// Last one in seventh line console.log (). |
18 | | - |
19 | | -// 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? |
20 | | -// The error is in line 5. The error is occurring because there is a missing comma in the replaceAll function. The correct code should be: priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); |
21 | | - |
22 | | -// c) Identify all the lines that are variable reassignment statements |
23 | | -// Line 4 and line 5 are variable reassignment statements. In line 4, carPrice is being reassigned with the new value after removing the comma and converting it to a number. In line 5, priceAfterOneYear is being reassigned in the same way. |
24 | | - |
25 | | -// d) Identify all the lines that are variable declarations |
26 | | -// Line 1 and line 2 are variable declarations. In line 1, carPrice is declared and initialized with the string value "10,000". In line 2, priceAfterOneYear is declared and initialized with the string value "8,543". |
27 | | - |
28 | | -// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? |
29 | | -// The expression removes the comma from the price string and converts it into a number. This allows JavaScript to use the value in calculations. |
| 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 | +// There are 5 function calls. In the fourth line Number () and replaceAll (). |
| 16 | +// In the fifth line same as line 4 Number () and replaceAll (). |
| 17 | +// Last one in seventh line console.log (). |
| 18 | + |
| 19 | +// 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? |
| 20 | +// The error is in line 5. The error is occurring because there is a missing comma in the replaceAll function. The correct code should be: priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); |
| 21 | + |
| 22 | +// c) Identify all the lines that are variable reassignment statements |
| 23 | +// Line 4 and line 5 are variable reassignment statements. In line 4, carPrice is being reassigned with the new value after removing the comma and converting it to a number. In line 5, priceAfterOneYear is being reassigned in the same way. |
| 24 | + |
| 25 | +// d) Identify all the lines that are variable declarations |
| 26 | +// Line 1 and line 2 are variable declarations. In line 1, carPrice is declared and initialized with the string value "10,000". In line 2, priceAfterOneYear is declared and initialized with the string value "8,543". |
| 27 | +// line 7 and line 8 are also variable declarations. In line 6, priceDifference is declared and initialized with the difference between carPrice and priceAfterOneYear. In line 7, percentageChange is declared and initialized with the calculated percentage change based on the price difference and carPrice. |
| 28 | + |
| 29 | +// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? |
| 30 | +// The expression removes the comma from the price string and converts it into a number. This allows JavaScript to use the value in calculations. |
0 commit comments