-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1-percentage-change.js
More file actions
64 lines (33 loc) · 2.21 KB
/
1-percentage-change.js
File metadata and controls
64 lines (33 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
let carPrice = "10,000";
let priceAfterOneYear = "8,543";
carPrice = Number(carPrice.replaceAll(",", ""));
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",",""));
const priceDifference = carPrice - priceAfterOneYear;
const percentageChange = (priceDifference / carPrice) * 100;
console.log(`The percentage change is ${percentageChange} %`);
// Read the code and then answer the questions below
// a) How many function calls are there in this file? Write down all the lines where a function call is made
/*There are four function calls in total: two in the line
carPrice = Number(carPrice.replaceAll(",", ""));
and two in the line
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
In each line, one function is nested inside the other, so both lines contain two function calls each.*/
// 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?
/*priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
^^^
SyntaxError: missing ) after argument list
the error is because a missing comma in the replaceAll method argument list
*/
// c) Identify all the lines that are variable reassignment statements
/*carPrice = Number(carPrice.replaceAll(",", ""));
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
these two lines are variable reassignment statements because we are reassigning a new value to the existing variables carPrice and priceAfterOneYear.*/
// d) Identify all the lines that are variable declarations
/*let carPrice = "10,000";
let priceAfterOneYear = "8,543";
const priceDifference = carPrice - priceAfterOneYear;
const percentageChange = (priceDifference / carPrice) * 100;
these four lines are variable declaration statements because we are declaring new variables:
carPrice, priceAfterOneYear, priceDifference and percentageChange using let and const keywords.*/
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
/*first removes all commas from the string carPrice using replaceAll(",", ""), and then converts the resulting string into a number using Number().*/