@@ -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 ;
@@ -12,11 +12,35 @@ console.log(`The percentage change is ${percentageChange}`);
1212// Read the code and then answer the questions below
1313
1414// a) How many function calls are there in this file? Write down all the lines where a function call is made
15+ there are 4 function calls in this File , they are in the following lines :
16+ line 1 : carPrice . replaceAll ( "," , "" )
17+ line 2 : priceAfterOneYear . replaceAll ( "," , "" )
18+ line 3 : Number ( carPrice . replaceAll ( "," , "" ) )
19+ line 4 : Number ( priceAfterOneYear . replaceAll ( "," , "" ) )
1520
1621// 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?
1722
1823// c) Identify all the lines that are variable reassignment statements
24+ there are 4 variables reassignment statements in this file , in the following lines:
25+ line 1 = carPrice =
26+ line 2 = priceAfterOneYear =
27+ line 3 = carPrice = Number ( carPrice . replaceAll ( "," , "" ) )
28+ line 4 = priceAfterOneYear = Number ( priceAfterOneYear . replaceAll ( "," , "" ) )
29+
30+
31+
1932
2033// d) Identify all the lines that are variable declarations
34+ there are 2 variable declarations in this file , in :
35+ line 1 : let carPrice =
36+ line 2 : let priceFterOneYear =
37+
2138
2239// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
40+
41+ the expression `Number(carPrice.replaceAll(",", ""))` is doing the following :
42+ 1. `carPrice.replaceAll(",", "")` removes all commas from the string value of `carPrice` .
43+ 2. `Number(...)` converts the resulting string ( which no longer contains commas ) into a number .
44+
45+ This is necessary because the original `carPrice` variable is a string with commas ( e . g . , "10,000" ) , and JavaScript cannot perform mathematical operations on strings with commas .
46+ The purpose of this expression is to convert the formatted price string into a numeric value that can be used in calculations .
0 commit comments