@@ -2,21 +2,25 @@ 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- // Read the code and then answer the questions below
12+ // a) Function calls:
13+ // Line 4: replaceAll(",", "") and Number(...)
14+ // Line 5: replaceAll(",", "") and Number(...)
15+ // Line 10: console.log(...)
1316
14- // a) How many function calls are there in this file? Write down all the lines where a function call is made
17+ // b) The error on line 5 is a missing comma between the two arguments to replaceAll:
18+ // replaceAll("," "") should be replaceAll(",", ""). Fixed above.
1519
16- // 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?
20+ // c) Variable reassignment statements: lines 4 and 5 (carPrice = ..., priceAfterOneYear = ...)
1721
18- // c) Identify all the lines that are variable reassignment statements
22+ // d) Variable declarations: lines 1, 2 (let), lines 7, 8 (const)
1923
20- // d) Identify all the lines that are variable declarations
21-
22- // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
24+ // e) Number(carPrice.replaceAll(",","")) first removes all commas from the string "10,000"
25+ // turning it into "10000", then converts that string into the number 10000.
26+ // This is necessary because you cannot do arithmetic on strings.
0 commit comments