@@ -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 ;
@@ -13,10 +13,31 @@ console.log(`The percentage change is ${percentageChange}`);
1313
1414// a) How many function calls are there in this file? Write down all the lines where a function call is made
1515
16+ // carPrice.replaceAll(",", "") (line 4)
17+ // Number(carPrice.replaceAll(",", "")) (line 4)
18+ // priceAfterOneYear.replaceAll(",", "") (line 5)
19+ // Number(priceAfterOneYear.replaceAll(",", "")) (line 5)
20+ // console.log(`The percentage change is ${percentageChange}`) (line 10)
21+
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?
1723
24+ // Syntax error - Arguments need to be separated by a comma
25+
1826// c) Identify all the lines that are variable reassignment statements
1927
28+ // carPrice = Number(carPrice.replaceAll(",", "")) (line 4)
29+ // priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")) (line 5)
30+
2031// d) Identify all the lines that are variable declarations
2132
33+ // let carPrice = "10,000" (line 1)
34+ // let priceAfterOneYear = "8,543" (line 2)
35+ // const priceDifference = carPrice - priceAfterOneYear (line 7)
36+ // const percentageChange = (priceDifference / carPrice) * 100 (line 8)
37+
2238// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
39+
40+ // (carPrice.replaceAll(",", "")) takes the string stored in the carPrice variable ("10,000") and removes all instances of commas (",")
41+ // Without this, the result of Number(carPrice) would be NaN (Not a Number)
42+
43+ // Number(...) then takes the edited string ("10000") and converts it to a number (10000) so that numerical operations can be performed on this value
0 commit comments