Skip to content

Commit 5b21ac9

Browse files
committed
1-percentage-change.js
1 parent fea47a8 commit 5b21ac9

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

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

Lines changed: 55 additions & 1 deletion
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;
@@ -20,3 +20,57 @@ console.log(`The percentage change is ${percentageChange}`);
2020
// d) Identify all the lines that are variable declarations
2121

2222
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
23+
24+
25+
/* SOLUTION
26+
27+
28+
a) **How many function calls are there in this file? Write down all the lines where a function call is made**
29+
There are **5 function calls**:
30+
1. `carPrice.replaceAll(",", "")`
31+
2. `Number(carPrice.replaceAll(",", ""))`
32+
3. `priceAfterOneYear.replaceAll("," "")` (should be `replaceAll(",", "")`)
33+
4. `Number(priceAfterOneYear.replaceAll("," ""))` (should be `replaceAll(",", "")`)
34+
5. `console.log(...)`
35+
36+
**Lines with function calls:**
37+
- `carPrice = Number(carPrice.replaceAll(",", ""));`
38+
- `priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));`
39+
- `console.log(...)`
40+
41+
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?**
42+
The error is on this line:
43+
```javascript
44+
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
45+
```
46+
There is a syntax error: `replaceAll("," "")` is missing a comma between the arguments.
47+
**Fix:**
48+
```javascript
49+
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
50+
```
51+
52+
c) **Identify all the lines that are variable reassignment statements**
53+
- `carPrice = Number(carPrice.replaceAll(",", ""));`
54+
- `priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));`
55+
56+
d) **Identify all the lines that are variable declarations**
57+
- `let carPrice = "10,000";`
58+
- `let priceAfterOneYear = "8,543";`
59+
- `const priceDifference = carPrice - priceAfterOneYear;`
60+
- `const percentageChange = (priceDifference / carPrice) * 100;`
61+
62+
e) **Describe what the expression `Number(carPrice.replaceAll(",", ""))` is doing - what is the purpose of this expression?**
63+
It removes all commas from the `carPrice` string (e.g., turns `"10,000"` into `"10000"`), then converts the result to a number (`10000`).
64+
This allows for mathematical operations on the price.
65+
66+
67+
68+
69+
70+
71+
72+
73+
74+
75+
76+
*/

0 commit comments

Comments
 (0)