Skip to content

Commit de2a36d

Browse files
committed
completed 1-percentage-change.js with comments
1 parent e05bbce commit de2a36d

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

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

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,47 @@
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(",", "")); // returns 10000
5+
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",",""));//returns 8543
6+
//priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
7+
// incorrect code missing syntax ie a comma
68

79
const priceDifference = carPrice - priceAfterOneYear;
810
const percentageChange = (priceDifference / carPrice) * 100;
911

10-
console.log(`The percentage change is ${percentageChange}`);
12+
console.log(`The percentage change is ${percentageChange}`); //returns The percentage change is 14.57
1113

1214
// Read the code and then answer the questions below
1315

14-
// a) How many function calls are there in this file? Write down all the lines where a function call is made
16+
// a) How many function calls are there in this file?
17+
// Write down all the lines where a function call is made
18+
//*Answer
19+
//There are 5 function calls: 2* Number(), 2*.replaceAll() and 1* console.log().
20+
//carPrice = Number(carPrice.replaceAll(",", ""));
21+
//priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",",""));
22+
//console.log(`The percentage change is ${percentageChange}`);
1523

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?
24+
// b) Run the code and identify the line where the error is coming from -
25+
// why is this error occurring? How can you fix this problem?
26+
//*Answer it is missing a comma on line 5.
27+
// Add the comma to fix it: ...replaceAll(",",""));
1728

1829
// c) Identify all the lines that are variable reassignment statements
30+
//*Answer
31+
//carPrice = Number(carPrice.replaceAll(",", ""));
32+
//priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
33+
1934

2035
// d) Identify all the lines that are variable declarations
36+
//let carPrice = "10,000";
37+
//let priceAfterOneYear = "8,543";
38+
//const priceDifference = carPrice - priceAfterOneYear;
39+
//const percentageChange = (priceDifference / carPrice) * 100;
40+
41+
42+
// e) Describe what the expression
43+
// Number(carPrice.replaceAll(",","")) is doing -
44+
// what is the purpose of this expression?
45+
//It converts the carPrice into a number and removes the spaces and commas.
46+
2147

22-
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?

0 commit comments

Comments
 (0)