@@ -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
15+ // all the lines where a function call is made
16+
17+ // There are 5 function calls in this file. They are on lines 1,
18+ // 2, 4, and 5. 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
26+ // from - why is 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
29+ // missing comma between the replaceAll "".
30+ // We can fix this by adding a comma between the two empty strings in
31+ // the replaceAll function on line 5, like this:
32+ // priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
33+
1534
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?
1735
1836// c) Identify all the lines that are variable reassignment statements
1937
38+ // carPrice = Number(carPrice.replaceAll(",", ""));
39+ // priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
40+
41+
42+
2043// d) Identify all the lines that are variable declarations
2144
22- // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
45+ // Lines 1,2,7 and 8.
46+ // let carPrice = "10,000";
47+ // let priceAfterOneYear = "8,543";
48+ // const priceDifference = carPrice - priceAfterOneYear;
49+ // const percentageChange = (priceDifference / carPrice) * 100;
50+
51+
52+
53+ // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing -
54+ // what is the purpose of this expression?
55+ // carPrice.replaceAll(",", "") - Replaces all commas "," in the string with nothing "".
56+ // 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 it into a
59+ // number (10000) that we can do math with.
0 commit comments