Skip to content

Commit 4004b30

Browse files
committed
Answer to 3-mandatory-interpret/1-percentage-change.js code updated and explanation included as text
1 parent 6b83e27 commit 4004b30

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
let carPrice = "10,000";
22
let priceAfterOneYear = "8,543";
33

4-
carPrice = Number(carPrice.replaceAll(",", ""));
5-
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
4+
//carPrice = Number(carPrice.replaceAll(",", ""));
5+
//priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
6+
7+
carPrice = Number(carPrice.replaceAll(",",""));
8+
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
9+
610

711
const priceDifference = carPrice - priceAfterOneYear;
812
const percentageChange = (priceDifference / carPrice) * 100;
@@ -12,11 +16,15 @@ console.log(`The percentage change is ${percentageChange}`);
1216
// Read the code and then answer the questions below
1317

1418
// a) How many function calls are there in this file? Write down all the lines where a function call is made
15-
19+
// There are 3 function calls in this file. They are on lines 4, 5, and 10. The function calls are: two uses of replaceAll,and one console.log.
1620
// 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?
21+
// The error is coming from lines 4 and 5 (which I have commed out). The error is occurring because there is a space between the comma and the closing quotation mark in the second argument of the replaceAll function. This causes the function to look for a comma followed by a space, which does not exist in the string. To fix this problem, have removed the space between the comma and the closing quotation mark in both lines, on lines 7&8.
1722

1823
// c) Identify all the lines that are variable reassignment statements
24+
// The variable reassignment statements are on lines 7 and 8, where carPrice and priceAfterOneYear are being reassigned to the result of the replaceAll function calls.
1925

2026
// d) Identify all the lines that are variable declarations
27+
// The variable declarations are on lines 1, 2, 11 and 12. They are: carPrice, priceAfterOneYear, priceDifference, and percentageChange.
2128

2229
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
30+
// The expression Number(carPrice.replaceAll(",","")) is first using the replaceAll function to remove all commas from the carPrice string, resulting in a string of digits. Then, the Number function is converting that string of digits into a numerical value. The purpose of this expression is to convert the carPrice from a formatted string (with commas) into a number that can be used for calculations.

0 commit comments

Comments
 (0)