@@ -12,11 +12,29 @@ 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+ // 5 function calls
16+ //Line 4: carPrice.replaceAll(",", "")
17+ //Line 4: Number(...) – wraps the above
18+ //Line 5: priceAfterOneYear.replaceAll(",", "") ✅ But this line has a syntax error (we’ll get to it)
19+ //Line 5: Number(...) – wraps the above
20+ //Line 10: console.log(...)
21+
1522
1623// 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?
24+ //syntax error! On line 5, there's a missing comma in the replaceAll method.
25+ //correct code should be
26+ //priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
1727
1828// c) Identify all the lines that are variable reassignment statements
29+ carPrice = Number ( carPrice . replaceAll ( "," , "" ) ) ;
30+ priceAfterOneYear = Number ( priceAfterOneYear . replaceAll ( "," , "" ) ) ;
1931
2032// d) Identify all the lines that are variable declarations
33+ let carPrice = "10,000" ;
34+ let priceAfterOneYear = "8,543" ;
2135
2236// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
37+ // This expression converts a string with commas into a number that can be used for mathematical calculations:
38+ // 1. carPrice.replaceAll(",", "") - removes all commas from the string "10,000" → "10000"
39+ // 2. Number(...) - converts the cleaned string "10000" into the number 10000
40+ // Purpose: Strings with commas cannot be used in math operations, so we need to clean and convert them to numbers first
0 commit comments