@@ -2,21 +2,43 @@ let carPrice = "10,000";
22let priceAfterOneYear = "8,543" ;
33
44carPrice = Number ( carPrice . replaceAll ( "," , "" ) ) ;
5- priceAfterOneYear = Number ( priceAfterOneYear . replaceAll ( "," "" ) ) ;
5+ priceAfterOneYear = Number ( priceAfterOneYear . replaceAll ( "," , "" ) ) ;
66
77const priceDifference = carPrice - priceAfterOneYear ;
88const percentageChange = ( priceDifference / carPrice ) * 100 ;
99
1010console . log ( `The percentage change is ${ percentageChange } ` ) ;
1111
12- // Read the code and then answer the questions below
13-
1412// a) How many function calls are there in this file? Write down all the lines where a function call is made
13+ // Function calls are when we use () like: something()
14+ // Line 4: carPrice.replaceAll(",", "")
15+ // Line 4: Number(...)
16+ // Line 5: priceAfterOneYear.replaceAll(",", "")
17+ // Line 5: Number(...)
18+ // Line 10: console.log(...)
19+ // Answer: 5 function calls
1520
1621// 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?
22+ // The error comes from the console.log line because it uses single quotes with ${percentageChange}
23+ // ${...} only works inside backticks `...` (template strings)
24+ // Fix: use backticks: console.log(`The percentage change is ${percentageChange}`);
1725
1826// c) Identify all the lines that are variable reassignment statements
27+ // Reassignment means we change an existing variable's value (no let/const on the line)
28+ // Line 4: carPrice = Number(carPrice.replaceAll(",", ""));
29+ // Line 5: priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
30+ // Answer: lines 4 and 5
1931
2032// d) Identify all the lines that are variable declarations
33+ // Declarations use let or const
34+ // Line 1: let carPrice = "10,000";
35+ // Line 2: let priceAfterOneYear = "8,543";
36+ // Line 7: const priceDifference = carPrice - priceAfterOneYear;
37+ // Line 8: const percentageChange = (priceDifference / carPrice) * 100;
38+ // Answer: lines 1, 2, 7 and 8
39+
40+ // e) Describe what the expression Number(carPrice.replaceAll(",", "")) is doing - what is the purpose of this expression?
41+ // replaceAll(",", "") removes commas from the string (e.g. "10,000" becomes "10000")
42+ // Number(...) converts the cleaned string into a real number so we can do maths with it
43+ // Purpose: turn "10,000" (text) into 10000 (number)
2144
22- // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
0 commit comments