Skip to content

Commit 7e75245

Browse files
committed
Change function replaceAll arguments adding commas to separate its parameters and use Number function to convert into a Number
1 parent 57e97e9 commit 7e75245

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

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

Lines changed: 31 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;
@@ -13,10 +13,40 @@ console.log(`The percentage change is ${percentageChange}`);
1313

1414
// a) How many function calls are there in this file? Write down all the lines where a function call is made
1515

16+
// Function calls and their line numbers
17+
// line 3 - carPrice.replaceAll(",", "") - Callls replaceAll() on the string to remove commas
18+
// line 3 - Number(...) - Converts the resulting string to a number
19+
// line 4 - priceAfterOneYear.replaceAll("," "") - Syntax error here - Intendend to replaceAll() but missing a comma between arguments
20+
// line 8 - console.log(...) - Prints to console
21+
// // Total intended function calls: 5
22+
1623
// 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?
1724

25+
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
26+
// Error: SyntaxError: missiong ) after argument list
27+
// Reason:
28+
// There's a missing comma between the arguments of replaceAll.
29+
// The correct syntax should be:
30+
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
31+
1832
// c) Identify all the lines that are variable reassignment statements
1933

34+
// These are the lines where variables that were already declared are assigned a new value:
35+
// carPrice = Number(carPrice.replaceAll(",", "");
36+
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
37+
// Reassignments: Lines 3 and 4
38+
2039
// d) Identify all the lines that are variable declarations
2140

41+
// These are the lines where variables are first introduced:
42+
// let carPrice = "10,000";
43+
// let priceAfterOneYear = "8,543";
44+
// const priceDifference = carPrice - priceAfterOneYear;
45+
// const percentageChange = (priceDifference / carPrice) * 100;
46+
2247
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
48+
49+
// carPrice.replaceAll(",", "") - removes all commas from the string "10,000", then it becomes "10000"
50+
// Number("10000") - Converts the string "10000" into a numeric value 10000.
51+
// Purpose: To convert a formatted currency string (with commas) into a number that can be used for mathematical calculations.
52+

0 commit comments

Comments
 (0)