@@ -2,7 +2,9 @@ let carPrice = "10,000";
22let priceAfterOneYear = "8,543" ;
33
44carPrice = Number ( carPrice . replaceAll ( "," , "" ) ) ;
5- priceAfterOneYear = Number ( priceAfterOneYear . replaceAll ( "," "" ) ) ;
5+ //priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); //error line of code
6+ priceAfterOneYear = Number ( priceAfterOneYear . replaceAll ( "," , "" ) ) ; //fixed
7+
68
79const priceDifference = carPrice - priceAfterOneYear ;
810const percentageChange = ( priceDifference / carPrice ) * 100 ;
@@ -12,11 +14,33 @@ console.log(`The percentage change is ${percentageChange}`);
1214// Read the code and then answer the questions below
1315
1416// a) How many function calls are there in this file? Write down all the lines where a function call is made
17+ /* There are three function calls in this file as shown by the following lines from the code:
18+ carPrice = Number(carPrice.replaceAll(",", ""));
19+ priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
20+ console.log(`The percentage change is ${percentageChange}`);
21+
22+ replaceAll and log are the functions in the above lines */
1523
1624// 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?
25+ /* The error is coming from the following line inside the replaceAll method that accepts two parameters separated by comma
26+ but in our code there is no comma for the separation and that is the problem
27+ priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
28+ we can fix this by simply adding the comma after first parameter value as follows:
29+ priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," , "")); */
1730
1831// c) Identify all the lines that are variable reassignment statements
32+ /* The following two lines are the variable reassignment statements:
33+ carPrice = Number(carPrice.replaceAll(",", ""));
34+ //priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); */
1935
2036// d) Identify all the lines that are variable declarations
37+ /* All the lines that are variable declarations are as follows:
38+ carPrice = Number(carPrice.replaceAll(",", ""));
39+ priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); //error line of code
40+ const priceDifference = carPrice - priceAfterOneYear;
41+ const percentageChange = (priceDifference / carPrice) * 100; */
2142
2243// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
44+ / * T h i s c o d e i s r e p l a c i n g a l l t h e c o m m a c h a r a c t e r s w i t h a n e m p t y c h a r a c t e r i n s i d e t h e c a r P r i c e S t r i n g a n d t h e n c o n v e r t i n g t h a t S t r i n g t o a N u m b e r
45+ The purpose of doing this is because in the later part of code we are doing some Math calculations on this number
46+ and , therefore , we can 't use String, we need to remove the commas in the String and convert that String into a number
0 commit comments