@@ -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 five function calls in this file . The lines where function calls are made are :
16+ 1. Line 4 : carPrice . replaceAll ( "," , "" )
17+ 2. Line 5 : priceAfterOneYear . replaceAll ( "," , "" )
18+ 3. Line 7 : Number ( carPrice . replaceAll ( "," , "" ) )
19+ 4. Line 8 : Number ( priceAfterOneYear . replaceAll ( "," , "" ) )
20+ 5. Line 10 : console . log ( )
1521
1622// 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?
1723
24+ The error is occurring on line 5 because the "," character was ommited in the replaceAll function , to fix the problem ,
25+ we need to include the "," character in the replaceAll function like this : priceAfterOneYear . replaceAll ( "," , "")
26+
1827// c) Identify all the lines that are variable reassignment statements
1928
29+ The lines that are variable reassignment statements are :
30+ 1. Line 4 : carPrice = Number ( carPrice . replaceAll ( "," , "" ) ) ;
31+ 2. Line 5 : priceAfterOneYear = Number ( priceAfterOneYear . replaceAll ( "," , "" ) ) ;
32+
2033// d) Identify all the lines that are variable declarations
34+ The lines that are variable declarations are :
35+ 1. Line 1 : let carPrice = "10,000" ;
36+ 2. Line 2 : let priceAfterOneYear = "8,543" ;
37+ 3. Line 6 : const priceDifference = carPrice - priceAfterOneYear ;
38+ 4. Line 7 : const percentageChange = ( priceDifference / carPrice ) * 100 ;
2139
2240// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
41+ The expression "Number ( carPrice . replaceAll ( "," , "" ) ) " is performing two main operations:
42+ 1. "carPrice . replaceAll ( "," , "" ) ": This part of the expression removes all commas from the string representation of
43+ " carPrice ". For example, it converts " 10 , 000 " to " 10000 ".
44+ 2. "Number ( priceAfterOneYear . replaceAll ( "," , "" ) ) ; ": This part converts the resulting string (after removing commas)
45+ into a numeric value . The purpose of this expression is to convert the string representation of a number that
46+ may contain commas into an actual numeric value that can be used for mathematical calculations .
0 commit comments