11let carPrice = "10,000" ;
22let priceAfterOneYear = "8,543" ;
33
4- carPrice = Number ( carPrice . replace ( / , / g , "" ) ) ;
5- priceAfterOneYear = Number ( priceAfterOneYear . replace ( / , / g , "" ) ) ;
4+ carPrice = Number ( carPrice . replaceAll ( "," , "" ) ) ;
5+ priceAfterOneYear = Number ( priceAfterOneYear . replaceAll ( "," , "" ) ) ;
66const priceDifference = carPrice - priceAfterOneYear ;
77const percentageChange = ( priceDifference / carPrice ) * 100 ;
88
@@ -11,12 +11,24 @@ console.log(`The percentage change is ${percentageChange}`);
1111// Read the code and then answer the questions below
1212
1313// a) How many function calls are there in this file? Write down all the lines where a function call is made
14- // There are 5 function calls: Number() twice, replaceAll() twice, and console.log().
14+ // There are 5 function calls:
15+ // Line 4: replaceAll(",", "") and Number()
16+ // Line 5: replaceAll(",", "") and Number()
17+ // Line 9: console.log()
18+
1519// 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?
16- // replaceAll might not work in older Node versions
20+ // The original code on line 5 was: priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
21+ // This causes a SyntaxError because there is a missing comma between the two arguments of replaceAll().
22+ // The original code had replaceAll("," "") but it should be replaceAll(",", "").
23+ // The comma between "," and "" is needed to separate the two arguments of the function.
24+ // The fix is simply adding the missing comma: replaceAll(",", "")
25+
1726// c) Identify all the lines that are variable reassignment statements
18- // Lines 4 and 5 are variable reassignment statements.
27+ // Lines 4 and 5 are variable reassignment statements (carPrice and priceAfterOneYear are reassigned using let).
28+
1929// d) Identify all the lines that are variable declarations
20- // Lines 1, 2, 7, and 8 are variable declarations.
30+ // Lines 1, 2, 6, 7 are variable declarations (using let or const).
31+
2132// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
22- // It removes the comma from the string and converts it into a Number type.
33+ // It removes all commas from the string "10,000" to get "10000",
34+ // then converts that string into the number 10000 using Number().
0 commit comments