1- let carPrice = "10,000" ;
2- let priceAfterOneYear = "8,543" ;
3-
4- carPrice = Number ( carPrice . replaceAll ( "," , "" ) ) ;
5- priceAfterOneYear = Number ( priceAfterOneYear . replaceAll ( "," "" ) ) ;
6-
7- const priceDifference = carPrice - priceAfterOneYear ;
8- const percentageChange = ( priceDifference / carPrice ) * 100 ;
9-
10- console . log ( `The percentage change is ${ percentageChange } ` ) ;
11-
12- // Read the code and then answer the questions below
13-
14- // a) How many function calls are there in this file? Write down all the lines where a function call is made
15-
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?
17-
18- // c) Identify all the lines that are variable reassignment statements
19-
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?
1+ let carPrice = "10,000" ;
2+ let priceAfterOneYear = "8,543" ;
3+
4+ carPrice = Number ( carPrice . replaceAll ( "," , "" ) ) ;
5+ priceAfterOneYear = Number ( priceAfterOneYear . replaceAll ( "," , "" ) ) ;
6+
7+ const priceDifference = carPrice - priceAfterOneYear ;
8+ const percentageChange = ( priceDifference / carPrice ) * 100 ;
9+
10+ console . log ( `The percentage change is ${ percentageChange } ` ) ;
11+
12+ // Read the code and then answer the questions below
13+
14+ // a) How many function calls are there in this file? Write down all the lines where a function call is made
15+
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?
17+
18+ // c) Identify all the lines that are variable reassignment statements
19+
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?
23+
24+ /*
25+
26+ a) How many function calls are there?
27+
28+ There are 5 function calls.
29+
30+ 1. carPrice.replaceAll(",", "")
31+ 2. Number(......)
32+ 3. priceAfterOneYear.replaceAll(",", "")
33+ 4. Number(......)
34+ 5. console.log(.....)
35+
36+ b) Why is the error occurring?
37+
38+ Because of the missing comma in the function call ReplaceAll() on line 5
39+
40+ c) Variable reassignments
41+
42+ 1. carPrice = Number(carPrice.replaceAll(",", ""));
43+ 2. priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
44+
45+ d) all the variable declarations
46+
47+ 1. let carPrice = "10,000";
48+ 2. let priceAfterOneYear = "8,543";
49+ 3. const priceDifference = carPrice - priceAfterOneYear;
50+ 4. const percentageChange = (priceDifference / carPrice) * 100;
51+
52+
53+ e)What is the expression doing?
54+
55+ The replaceAll() removes commas from the string and Number() function call converts the string to a number.
56+
57+ The expression removes the commas from the price string and converts the result into a number so it can be used in mathematical calculations.
58+
59+
60+ */
0 commit comments