@@ -2,7 +2,7 @@ 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 ;
@@ -12,11 +12,27 @@ console.log(`The percentage change is ${percentageChange}`);
1212// Read the code and then answer the questions below
1313
1414// 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:
16+ // - line 4: carPrice.replaceAll(",", "")
17+ // - line 5: priceAfterOneYear.replaceAll(",", "")
18+ // - line 4: Number(...)
19+ // - line 5: Number(...)
20+ // - line 9: console.log(...)
1521
1622// 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?
23+ // The error occurs at line 5 because the syntax is invalid: replaceAll("," "") is missing a comma between the arguments.
24+ // Fix it by writing replaceAll(",", "").
1725
1826// c) Identify all the lines that are variable reassignment statements
27+ // - line 4: carPrice = Number(...)
28+ // - line 5: priceAfterOneYear = Number(...)
1929
2030// d) Identify all the lines that are variable declarations
31+ // - line 1: let carPrice = "10,000";
32+ // - line 2: let priceAfterOneYear = "8,543";
33+ // - line 7: const priceDifference = carPrice - priceAfterOneYear;
34+ // - line 8: const percentageChange = (priceDifference / carPrice) * 100;
2135
2236// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
37+ // It removes the commas from the string, then converts the cleaned string into a number.
38+ // This is necessary because the prices are stored as strings like "10,000" and "8,543", and we need to perform arithmetic on them.
0 commit comments