Skip to content

Commit 35cbc03

Browse files
committed
Complete percentage-change interpret exercise
1 parent c115512 commit 35cbc03

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,35 @@ 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;
99

10-
console.log(`The percentage change is ${percentageChange}`);
10+
console.log(`The price difference is ${priceDifference}`);
11+
12+
13+
console.log(`The percentage change is ${percentageChange}%`);
1114

1215
// Read the code and then answer the questions below
1316

1417
// a) How many function calls are there in this file? Write down all the lines where a function call is made
18+
// Answer: 6 function calls.
1519

1620
// 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?
21+
// Answer: line 5 {replaceAll(",", ""))} added , to fix the SyntaxError.
1722

1823
// c) Identify all the lines that are variable reassignment statements
24+
// Answer: carPrice = Number(carPrice.replaceAll(",", "")); line 4
25+
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); line 5
1926

2027
// d) Identify all the lines that are variable declarations
28+
// Answer: let carPrice = "10,000"; line1
29+
// let priceAfterOneYear = "8,543"; line 2
30+
// const priceDifference = carPrice - priceAfterOneYear; line 7
31+
// const percentageChange = (priceDifference / carPrice) * 100; line 8
2132

2233
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
34+
// Answer: carPrice.replaceAll(",","") remove commas from the string.
35+
// Number(...) changes the text to a number so subtraction and division work correctly.
36+

0 commit comments

Comments
 (0)