Skip to content

Commit 1dd8029

Browse files
fix: correct syntax error in priceAfterOneYear conversion and update comments
1 parent 1b5ea36 commit 1dd8029

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

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

Lines changed: 17 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;
@@ -12,11 +12,27 @@ console.log(`The percentage change is ${percentageChange}`);
1212
// Read the code and then answer the questions below
1313

1414
// a) How many function calls are there in this file? Write down all the lines where a function call is made
15+
// Answer: there is three functions being called in this file.
16+
// 1. Number(carPrice.replaceAll(",", "")) which is on line 4.
17+
// 2. Number(priceAfterOneYear.replaceAll("," "")) which is on line 5.
18+
// 3. console.log(`The percentage change is ${percentageChange}`) which is on line 10.
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: An error occurred on line 5 because there was a missing comma in the function call and that was causing a syntax error.
22+
// To fix this I added the missing comma in the function call on line 5.
1723

1824
// c) Identify all the lines that are variable reassignment statements
25+
// Answer: there are two variable files are reassignment statements in this file. as we declared the variables on lines 1 and 2 with the let statements.
26+
// 1. carPrice = Number(carPrice.replaceAll(",", "")); on line 4.
27+
// 2. priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); on line 5.
1928

2029
// d) Identify all the lines that are variable declarations
30+
// Answer: there is four variables declared in this file. as they are defined with the statements "let and const".
31+
// 1. let carPrice = "10,000"; on line 1.
32+
// 2. let priceAfterOneYear = "8,543"; on line 2.
33+
// 3. const priceDifference = carPrice - priceAfterOneYear; on line 7.
34+
// 4. const percentageChange = (priceDifference / carPrice) * 100; on line 8.
2135

2236
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
37+
// Answer: this expression converts the string value of carPrice which contains a comma into a number by first removing the comma using the replaceAll method and then converting the resulting string to a number using the Number function. This is necessary because mathematical operations require numeric values and the original value of carPrice is a string with a comma, which would not work correctly in calculations.
38+
// its purpose is to ensure that the carPrice variable holds a numeric value so that math can be done with the code and be able to work out the percentages.

0 commit comments

Comments
 (0)