You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
console.log(`The percentage change is ${percentageChange}`);
11
11
12
-
// Read the code and then answer the questions below
13
-
14
12
// a) How many function calls are there in this file? Write down all the lines where a function call is made
13
+
// There are five function calls: carPrice.replaceAll(",", ""), Number(carPrice.replaceAll(",", "")), priceAfterOneYear.replaceAll(",", ""), Number(priceAfterOneYear.replaceAll(",", "")), and console.log(...).
15
14
16
15
// 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?
16
+
// The error comes from the line priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); because there is a missing comma between the arguments in replaceAll, which causes a syntax error. It can be fixed by writing replaceAll(",", "") instead.
17
17
18
18
// c) Identify all the lines that are variable reassignment statements
19
+
// The variable reassignment statements are carPrice = Number(carPrice.replaceAll(",", "")); and priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));.
19
20
20
21
// d) Identify all the lines that are variable declarations
22
+
// The variable declarations are let carPrice = "10,000";, let priceAfterOneYear = "8,543";, const priceDifference = carPrice - priceAfterOneYear;, and const percentageChange = (priceDifference / carPrice) * 100;.
21
23
22
-
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
24
+
// e) Describe what the expression Number(carPrice.replaceAll(",", "")) is doing - what is the purpose of this expression?
25
+
// The expression first removes the comma from the string "10,000" using replaceAll(",", ""), resulting in "10000", and then converts that string into a number using Number(), so that it can be used in mathematical calculations.
0 commit comments