@@ -2,21 +2,40 @@ 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+
1213// Read the code and then answer the questions below
1314
1415// a) How many function calls are there in this file? Write down all the lines where a function call is made
15-
16+ //there are 5 functions calls in line 4, 5 and 10
17+ //carPrice = Number(carPrice.replaceAll(",", ""));: Number(), replaceAll()
18+ //priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));:Number(), replaceAll()
19+ //console.log(`The percentage change is ${percentageChange}`); :console.log()
20+
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?
17-
22+ //The error was coming from line 5:
23+ //priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
24+ //The error occurred because there was a missing comma between the two arguments inside the replaceAll() function.
25+ //To fix the problem, we add the missing comma: priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
1826// c) Identify all the lines that are variable reassignment statements
19-
27+ // Line 4: carPrice = Number(carPrice.replaceAll(",", ""));
28+ // Line 5: priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
2029// d) Identify all the lines that are variable declarations
30+ // Line 1: let carPrice = "10,000";
31+ // Line 2: let priceAfterOneYear = "8,543";
32+ // Line 7: const priceDifference = carPrice - priceAfterOneYear;
33+ // Line 8: const percentageChange = (priceDifference / carPrice) * 100;
2134
2235// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
36+ // The expression Number(carPrice.replaceAll(",", "")) first removes all commas
37+ // from the string stored in carPrice using replaceAll().
38+ // For example, "10,000" becomes "10000".
39+ // Then Number() converts the cleaned string into a numeric value.
40+ // The purpose of this expression is to convert a formatted string price
41+ // into a number so that mathematical calculations can be performed.
0 commit comments