@@ -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,37 @@ 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: There are 5 function calls in total, 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+ // Answer:
25+ // The red line on replaceAll("," "") indicates that there is a syntax error on line 5, as shown in the code.
26+ // A comma was missing between the two arguments inside the replaceAll function call.
27+ // To fix this, add a comma between the two arguments like this: replaceAll(",", "").
28+
29+ // Error: SyntaxError: missing ) after argument list and price AfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
30+ // The error appears on the console.log line because the string was written using smart quotes instead of normal JavaScript quotes.
31+ // Smart quotes break the strong syntax rules of JavaScript.
32+ // To fix this, replace the smart quotes with straight quotes.
1733
1834// c) Identify all the lines that are variable reassignment statements
35+ // carPrice = Number(carPrice.replaceAll(",", ""));
36+ // priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
37+ // Answer: Lines 4 and 5 are variable reassignment statements.
1938
2039// d) Identify all the lines that are variable declarations
40+ // let carPrice = "10,000";
41+ // let priceAfterOneYear = "8,543";
42+ // const priceDifference = carPrice - priceAfterOneYear;
43+ // const percentageChange = (priceDifference / carPrice) * 100;
44+ // Answer: Lines 1, 2, 7 and 8 are variable declarations.
2145
2246// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
47+ // The expression Number(carPrice.replaceAll(",", "")) is removing all the commas from carPrice string by replacing every comma with an empty string.
48+ // The purpose is to clean the value so that it can be converted to a proper number using Number().
0 commit comments