Skip to content

Commit 6a10112

Browse files
committed
Complete percentage change interpretation
1 parent f34c0da commit 6a10112

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

‎Sprint-2/3-mandatory-interpret/1-percentage-change.js‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ let carPrice = "10,000";
22
let priceAfterOneYear = "8,543";
33

44
carPrice = Number(carPrice.replaceAll(",", ""));
5-
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
5+
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
66

77
const priceDifference = carPrice - priceAfterOneYear;
88
const percentageChange = (priceDifference / carPrice) * 100;
@@ -12,11 +12,27 @@ 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 5 function calls:
16+
// - line 4: carPrice.replaceAll(",", "")
17+
// - line 5: priceAfterOneYear.replaceAll(",", "")
18+
// - line 4: Number(...)
19+
// - line 5: Number(...)
20+
// - line 9: 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?
23+
// The error occurs at line 5 because the syntax is invalid: replaceAll("," "") is missing a comma between the arguments.
24+
// Fix it by writing replaceAll(",", "").
1725

1826
// c) Identify all the lines that are variable reassignment statements
27+
// - line 4: carPrice = Number(...)
28+
// - line 5: priceAfterOneYear = Number(...)
1929

2030
// d) Identify all the lines that are variable declarations
31+
// - line 1: let carPrice = "10,000";
32+
// - line 2: let priceAfterOneYear = "8,543";
33+
// - line 7: const priceDifference = carPrice - priceAfterOneYear;
34+
// - line 8: const percentageChange = (priceDifference / carPrice) * 100;
2135

2236
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
37+
// It removes the commas from the string, then converts the cleaned string into a number.
38+
// This is necessary because the prices are stored as strings like "10,000" and "8,543", and we need to perform arithmetic on them.

0 commit comments

Comments
 (0)