-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1-percentage-change.js
More file actions
40 lines (30 loc) · 1.89 KB
/
1-percentage-change.js
File metadata and controls
40 lines (30 loc) · 1.89 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
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
// 5 function calls
//Line 4: carPrice.replaceAll(",", "")
//Line 4: Number(...) – wraps the above
//Line 5: priceAfterOneYear.replaceAll(",", "") ✅ But this line has a syntax error (we’ll get to it)
//Line 5: Number(...) – wraps the above
//Line 10: console.log(...)
// 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?
//syntax error! On line 5, there's a missing comma in the replaceAll method.
//correct code should be
//priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
// c) Identify all the lines that are variable reassignment statements
carPrice = Number(carPrice.replaceAll(",", ""));
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
// d) Identify all the lines that are variable declarations
let carPrice = "10,000";
let priceAfterOneYear = "8,543";
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
// This expression converts a string with commas into a number that can be used for mathematical calculations:
// 1. carPrice.replaceAll(",", "") - removes all commas from the string "10,000" → "10000"
// 2. Number(...) - converts the cleaned string "10000" into the number 10000
// Purpose: Strings with commas cannot be used in math operations, so we need to clean and convert them to numbers first