@@ -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 ;
@@ -11,12 +11,49 @@ console.log(`The percentage change is ${percentageChange}`);
1111
1212// Read the code and then answer the questions below
1313
14- // a) How many function calls are there in this file? Write down all the lines where a function call is made
14+ // a) How many function calls are there in this file? Write down all the
15+ // lines where a function call is made
16+
17+ // There are 5 function calls in this file. They are on lines 1, 2, 4, and 5.
18+ // The functions being called are replaceAll() and Number().
19+ // Line 4: replaceAll and Number (2)
20+ // Line 5: replaceAll and Number (2)
21+ // Line 10: console.log (1)
22+
23+
24+
25+ // b) Run the code and identify the line where the error is coming from - why is
26+ // this error occurring? How can you fix this problem?
27+
28+ // The error is occuring on line 5. It is occuring because there is a missing
29+ // comma between the replaceAll "".
30+ // We can fix this by adding a comma between the two empty strings in the replaceAll
31+ // function on line 5, like this: priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
32+
1533
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?
1734
1835// c) Identify all the lines that are variable reassignment statements
1936
37+ // carPrice = Number(carPrice.replaceAll(",", ""));
38+ // priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
39+
40+
41+
2042// d) Identify all the lines that are variable declarations
2143
22- // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
44+ // Lines 1,2,7 and 8.
45+ // let carPrice = "10,000";
46+ // let priceAfterOneYear = "8,543";
47+ // const priceDifference = carPrice - priceAfterOneYear;
48+ // const percentageChange = (priceDifference / carPrice) * 100;
49+
50+
51+
52+ // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing -
53+ // what is the purpose of this expression?
54+
55+ // carPrice.replaceAll(",", "") - Replaces all commas "," in the string with
56+ // nothing "". e.g "10,000" becomes "10000".
57+ // Number() - Converts the resulting string "10000" into the number 10000.
58+ // The purpose of this is to take a string with commas (like "10,000") and turn
59+ // it into a number (10000) that we can do math with.
0 commit comments