@@ -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 ;
@@ -13,10 +13,40 @@ console.log(`The percentage change is ${percentageChange}`);
1313
1414// a) How many function calls are there in this file? Write down all the lines where a function call is made
1515
16+ // Function calls and their line numbers
17+ // line 3 - carPrice.replaceAll(",", "") - Callls replaceAll() on the string to remove commas
18+ // line 3 - Number(...) - Converts the resulting string to a number
19+ // line 4 - priceAfterOneYear.replaceAll("," "") - Syntax error here - Intendend to replaceAll() but missing a comma between arguments
20+ // line 8 - console.log(...) - Prints to console
21+ // // Total intended function calls: 5
22+
1623// 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?
1724
25+ // priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
26+ // Error: SyntaxError: missiong ) after argument list
27+ // Reason:
28+ // There's a missing comma between the arguments of replaceAll.
29+ // The correct syntax should be:
30+ // priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
31+
1832// c) Identify all the lines that are variable reassignment statements
1933
34+ // These are the lines where variables that were already declared are assigned a new value:
35+ // carPrice = Number(carPrice.replaceAll(",", "");
36+ // priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
37+ // Reassignments: Lines 3 and 4
38+
2039// d) Identify all the lines that are variable declarations
2140
41+ // These are the lines where variables are first introduced:
42+ // let carPrice = "10,000";
43+ // let priceAfterOneYear = "8,543";
44+ // const priceDifference = carPrice - priceAfterOneYear;
45+ // const percentageChange = (priceDifference / carPrice) * 100;
46+
2247// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
48+
49+ // carPrice.replaceAll(",", "") - removes all commas from the string "10,000", then it becomes "10000"
50+ // Number("10000") - Converts the string "10000" into a numeric value 10000.
51+ // Purpose: To convert a formatted currency string (with commas) into a number that can be used for mathematical calculations.
52+
0 commit comments