Skip to content

Commit 850edaa

Browse files
committed
doc: documented the process to convert string in number to make math operations
1 parent b9b2ba8 commit 850edaa

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

Sprint-1/interpret/percentage-change.js

Lines changed: 44 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,53 @@ 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+
// There are 6 function calls in this file:
17+
// Line 5: carPrice.replaceAll(",", "")
18+
// Line 5: Number(carPrice.replaceAll(",", ""))
19+
// Line 6: priceAfterOneYear.replaceAll(",", "")
20+
// Line 6: Number(priceAfterOneYear.replaceAll(",", ""))
21+
// Line 10: console.log(`The percentage change is ${percentageChange}`) this is also
22+
// a function call to log the output as string to the console, then call the var percentageChange
23+
24+
1625
// 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?
26+
27+
// The error was line 5, carPrice.replaceAll("," ""); missing the comma between the arguments
1728

1829
// c) Identify all the lines that are variable reassignment statements
1930

31+
//line 4, line 5,
32+
2033
// d) Identify all the lines that are variable declarations
2134

35+
// line 1, line 2, line 7 and line 8
36+
37+
2238
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
39+
40+
// This expresion will work in the parenthesis first, replacing all commas in the string carPrice
41+
// with an empty string, so basically returning the string 10000 instead of "10,000".
42+
// Then the Number() function will convert that string "10000" into a number 10000.
43+
// secondly, js can not treat strings with commas as numbers, so we need to remove the commas
44+
// now with the number cleaned we can convert this string in number with the method Number().
45+
46+
47+
48+
49+
50+
51+
52+
// let carPrice = "10,000";
53+
// let priceAfterOneYear = "8,543";
54+
55+
// carPrice = Number(carPrice.replaceAll(",", ""));
56+
// console.log(carPrice); // the commas are removed and the string is converted to a number
57+
58+
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
59+
// console.log(priceAfterOneYear); // the commas are removed and the string is converted to a number
60+
61+
// const priceDifference = carPrice - priceAfterOneYear;
62+
// console.log(`The price difference is ${priceDifference}`);
63+
64+
// const percentageChange = (priceDifference / carPrice) * 100;
65+
// console.log(`The percentage change is ${percentageChange}`);

0 commit comments

Comments
 (0)