Skip to content

Commit 9c5fc57

Browse files
committed
1-percentage-change.js completed
1 parent bd71230 commit 9c5fc57

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

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

Lines changed: 41 additions & 4 deletions
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;
@@ -11,12 +11,49 @@ console.log(`The percentage change is ${percentageChange}`);
1111

1212
// Read the code and then answer the questions below
1313

14-
// a) How many function calls are there in this file? Write down all the lines where a function call is made
14+
// a) How many function calls are there in this file? Write down
15+
// all the lines where a function call is made
16+
17+
// There are 5 function calls in this file. They are on lines 1,
18+
// 2, 4, and 5. The functions being called are replaceAll() and Number().
19+
// Line 4: replaceAll and Number (2)
20+
// Line 5: replaceAll and Number (2)
21+
// Line 10: console.log (1)
22+
23+
24+
25+
// b) Run the code and identify the line where the error is coming
26+
// from - why is this error occurring? How can you fix this problem?
27+
28+
// The error is occuring on line 5. It is occuring because there is a
29+
// missing comma between the replaceAll "".
30+
// We can fix this by adding a comma between the two empty strings in
31+
// the replaceAll function on line 5, like this:
32+
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
33+
1534

16-
// 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?
1735

1836
// c) Identify all the lines that are variable reassignment statements
1937

38+
// carPrice = Number(carPrice.replaceAll(",", ""));
39+
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
40+
41+
42+
2043
// d) Identify all the lines that are variable declarations
2144

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

0 commit comments

Comments
 (0)