Skip to content

Commit 978fdbf

Browse files
committed
Update 1-percentage-change.js
Answered all questions and added , to the function.
1 parent c611ae5 commit 978fdbf

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

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

Lines changed: 30 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;
@@ -13,10 +13,39 @@ 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+
// There are 5 function calls in this file. They are on lines 1, 2, 4, and 5. The functions being called are replaceAll() and Number().
17+
// Line 4: replaceAll and Number (2)
18+
// Line 5: replaceAll and Number (2)
19+
// Line 10: console.log (1)
20+
21+
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+
// The error is occuring on line 5. It is occuring because there is a missing comma between the replaceAll "".
26+
// We can fix this by adding a comma between the two empty strings in the replaceAll function on line 5, like this: priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
27+
28+
29+
1830
// c) Identify all the lines that are variable reassignment statements
1931

32+
// carPrice = Number(carPrice.replaceAll(",", ""));
33+
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
34+
35+
36+
2037
// d) Identify all the lines that are variable declarations
2138

39+
// Lines 1,2,7 and 8.
40+
// let carPrice = "10,000";
41+
// let priceAfterOneYear = "8,543";
42+
// const priceDifference = carPrice - priceAfterOneYear;
43+
// const percentageChange = (priceDifference / carPrice) * 100;
44+
45+
46+
2247
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
48+
49+
// carPrice.replaceAll(",", "") - Replaces all commas "," in the string with nothing "". e.g "10,000" becomes "10000".
50+
// Number() - Converts the resulting string "10000" into the number 10000.
51+
// The purpose of this is to take a string with commas (like "10,000") and turn it into a number (10000) that we can do math with.

0 commit comments

Comments
 (0)