@@ -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,34 @@ 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+ // carPrice.replaceAll(",", "") - line 4
16+ // Number(...) - line 4, wraps the result of replaceAll
17+ // priceAfterOneYear.replaceAll("," "") - line 5
18+ // Number(...) - line 5, wraps the second result of replaceAll
19+ // console.log(..) - line 9
20+ // Answer: Overall, there are 5 function calls, on lines 4, 5 and 9.
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+ // Error: priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
24+ // The red line on replaceAll("," "") indicates that there is a syntax error on line 5.
25+ // A comma was missing between the two arguments inside the replaceAll function call.
26+ // To fix this, I added a comma between the two arguments like this: replaceAll(",", "").
27+
28+ // Error: SyntaxError: missing ) after argument list and price AfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
29+ // The error appears on the console.log line because the string was written using smart quotes instead of normal JavaScript quotes.
30+ // Smart quotes break the strong syntax rules of JavaScript.
31+ // To fix this, replace the smart quotes with straight quotes.
1732
1833// c) Identify all the lines that are variable reassignment statements
34+ // carPrice = Number(carPrice.replaceAll(",", "")); Line 4
35+ // priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); Line 5
1936
2037// d) Identify all the lines that are variable declarations
38+ // let carPrice = "10,000"; Line 1
39+ // let priceAfterOneYear = "8,543"; Line 2
40+ // const priceDifference = carPrice - priceAfterOneYear; Line 7
41+ // const percentageChange = (priceDifference / carPrice) * 100; Line 8
2142
2243// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
44+ // The expression Number(carPrice.replaceAll(",", "")) is removing all the commas from carPrice string by replacing every comma with an empty string.
45+ // The purpose is to clean the value so that it can be converted to a proper number using Number().
0 commit comments