@@ -2,21 +2,55 @@ let carPrice = "10,000";
22let priceAfterOneYear = "8,543" ;
33
44carPrice = Number ( carPrice . replaceAll ( "," , "" ) ) ;
5- priceAfterOneYear = Number ( priceAfterOneYear . replaceAll ( "," "" ) ) ;
5+ priceAfterOneYear = Number ( priceAfterOneYear . replaceAll ( "," , "" ) ) ;
6+
67
78const priceDifference = carPrice - priceAfterOneYear ;
89const percentageChange = ( priceDifference / carPrice ) * 100 ;
910
11+
1012console . log ( `The percentage change is ${ percentageChange } ` ) ;
1113
12- // Read the code and then answer the questions below
1314
14- // a) How many function calls are there in this file? Write down all the lines where a function call is made
15+ /*
16+ Read the code and then answer the questions below
17+
18+ a) How many function calls are there in this file? Write down all the lines where a function call is made
19+
20+ -------- Number(carPrice.replaceAll(",", "")); this convert string in numbers
21+ -------- replaceAll(",", "") this line is removing the "," and returning just the numbers
22+ -------- console.log(`The percentage change is ${percentageChange}`); this is a function to print what we call inside.
23+
24+
25+
26+ 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?
27+
28+ ------- In the line 5 we were missing the "," dividing the ("," "") this gave an error.
29+
30+
31+ c) Identify all the lines that are variable reassignment statements
32+
33+ ------- carPrice = Number(carPrice.replaceAll(",", ""));
34+ ------- priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
35+
36+
37+ d) Identify all the lines that are variable declarations
38+
39+ ------- let carPrice = "10,000";
40+ ------- let priceAfterOneYear = "8,543";
41+ ------- const priceDifference = carPrice - priceAfterOneYear;
42+ ------- const percentageChange = (priceDifference / carPrice) * 100;
43+
44+ e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
1545
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?
46+ ------- The expression Number(carPrice.replaceAll(",", "")) performs two actions:
47+ * carPrice.replaceAll(",", ""):
48+ The replaceAll() method is used on the string carPrice. It removes all commas "," from the
49+ string by replacing them with an empty string "".
1750
18- // c) Identify all the lines that are variable reassignment statements
51+ * Number(...);
52+ this function is then called on the result of replaceAll(),
53+ converting the string now without commas into a numeric value eg 10,000 => 10000
1954
20- // d) Identify all the lines that are variable declarations
2155
22- // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
56+ */
0 commit comments