|
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 five function calls in this code in lines 4, 5, and 10 |
16 | | -// These function calls are Number, replaceAll, and console.log |
17 | | - |
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 | | -// The error originally came from the replaceAll call for priceAfterOneYear: the two arguments |
20 | | -// (",", "") were missing a comma between them, causing a SyntaxError. |
21 | | -// The fix was adding the missing comma so replaceAll(",", "") has two properly separated arguments. |
22 | | - |
23 | | -// c) Identify all the lines that are variable reassignment statements |
24 | | -// Variable reassignment statements on lines 4 and 5. |
25 | | -// Variables carPrice and priceAfterOnYear are originally declared on lines 1 and 2, and reassigned on lines 4 and 5. |
26 | | - |
27 | | -// d) Identify all the lines that are variable declarations |
28 | | -// They are lines 1,2,7 and 8. |
29 | | -// On lines 1 and 2, variables carPrice and priceAfterOneYear are declared by let. |
30 | | -// On lines 7 and 8, variables priceDifference and percentageChange are declared by const. |
31 | | - |
32 | | -// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? |
33 | | -// replaceAll(",", "") removes all comma characters from the string, since commas aren't valid in a numeric value. |
34 | | -// Number() then converts the resulting clean string into an actual number, so it can be used in arithmetic. |
| 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 five function calls in this code in lines 4, 5, and 10 |
| 16 | +// These function calls are Number, replaceAll, and console.log |
| 17 | + |
| 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 | +// The error originally came from the replaceAll call for priceAfterOneYear: the two arguments |
| 20 | +// (",", "") were missing a comma between them, causing a SyntaxError. |
| 21 | +// The fix was adding the missing comma so replaceAll(",", "") has two properly separated arguments. |
| 22 | + |
| 23 | +// c) Identify all the lines that are variable reassignment statements |
| 24 | +// Variable reassignment statements on lines 4 and 5. |
| 25 | +// Variables carPrice and priceAfterOnYear are originally declared on lines 1 and 2, and reassigned on lines 4 and 5. |
| 26 | + |
| 27 | +// d) Identify all the lines that are variable declarations |
| 28 | +// They are lines 1,2,7 and 8. |
| 29 | +// On lines 1 and 2, variables carPrice and priceAfterOneYear are declared by let. |
| 30 | +// On lines 7 and 8, variables priceDifference and percentageChange are declared by const. |
| 31 | + |
| 32 | +// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? |
| 33 | +// replaceAll(",", "") removes all comma characters from the string, since commas aren't valid in a numeric value. |
| 34 | +// Number() then converts the resulting clean string into an actual number, so it can be used in arithmetic. |
0 commit comments