Skip to content

Commit ffaed93

Browse files
debug2604debug2604
authored andcommitted
Answered 1-percentage-change.js
1 parent 09045cf commit ffaed93

1 file changed

Lines changed: 33 additions & 22 deletions

File tree

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
1-
let carPrice = "10,000";
2-
let priceAfterOneYear = "8,543";
3-
4-
carPrice = Number(carPrice.replaceAll(",", ""));
5-
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
6-
7-
const priceDifference = carPrice - priceAfterOneYear;
8-
const percentageChange = (priceDifference / carPrice) * 100;
9-
10-
console.log(`The percentage change is ${percentageChange}`);
11-
12-
// Read the code and then answer the questions below
13-
14-
// a) How many function calls are there in this file? Write down all the lines where a function call is made
15-
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?
17-
18-
// c) Identify all the lines that are variable reassignment statements
19-
20-
// d) Identify all the lines that are variable declarations
21-
22-
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
1+
let carPrice = "10,000";
2+
let priceAfterOneYear = "8,543";
3+
4+
carPrice = Number(carPrice.replaceAll(",", ""));
5+
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
6+
7+
const priceDifference = carPrice - priceAfterOneYear;
8+
const percentageChange = (priceDifference / carPrice) * 100;
9+
10+
console.log(`The percentage change is ${percentageChange}`);
11+
12+
// Read the code and then answer the questions below
13+
14+
// a) How many function calls are there in this file? Write down all the lines where a function call is made
15+
// There are five function calls in this code in lines 4, 5, and 10
16+
// These function calls are Number, replaceAll, and console.log
17+
18+
// 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?
19+
// The error is from line 5:
20+
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
21+
// There should be a comma to separate arguments
22+
23+
// c) Identify all the lines that are variable reassignment statements
24+
// Variable reassingment statatments on lines 4 and 5.
25+
// Variables carPrice and priceAfterOnYear are originally declared on lines 1 and 2, and reassigned on lines 4 and 5.
26+
27+
// d) Identify all the lines that are variable declarations
28+
// They are lines 1,2,7 and 8.
29+
// On lines 1 and 2, variables carPrice and priceAfterOneYear are declared by let.
30+
// On lines 7 and 8, variables priceDifference and percentageChange are declared by const.
31+
32+
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
33+
// To remove comma as a punctuation and space such that the string is ready turn into a number by method Number.

0 commit comments

Comments
 (0)