Skip to content

Commit 7c0fea9

Browse files
committed
fix 3.js task under mandatory -error and also fix percentage -change .js based on the comment given.
1 parent 97d3425 commit 7c0fea9

2 files changed

Lines changed: 23 additions & 23 deletions

File tree

Sprint-1/2-mandatory-errors/3.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
const cardNumber = "4533787178994213";
2-
const last4Digits = cardNumber.slice(-4);
3-
console.log(last4Digits);
1+
const cardNumber = 4533787178994213;
2+
const last4Digits = String(cardNumber).slice(-4);
3+
console.log(last4Digits);
44
// The last4Digits variable should store the last 4 digits of cardNumber
55
// However, the code isn't working
66
// Before running the code, make and explain a prediction about why the code won't work
7-
// i predict that the code doesn't work its because of two things, first i thought its case sensitive things
8-
// the second one is the type of variable, i thought the slice method is only for string type variable.
7+
// i predict that the code doesn't work its because of two things, first i thought its case sensitive things
8+
// the second one is the type of variable, i thought the slice method is only for string type variable.
99

1010
// Then run the code and see what error it gives.
1111
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
12-
/*
12+
/*
1313
I run the code and it gives me an error that says "TypeError: cardNumber.slice is not a function".
1414
so i checked the type of cardNumber using typeof operator and it returns number.
1515
*/

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

Lines changed: 17 additions & 17 deletions
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,27 +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-
// there are 5 function calls in this file. including methods used , 2 methods = replaceAll(), methods are called under their object, replaceAll() is called under the string object.
16-
// The lines where a function call is made are:
17-
// Line 4: carPrice.replaceAll(",", "")
18-
// Line 5: priceAfterOneYear.replaceAll(",", "")
19-
// Line 7: console.log(`The percentage change is ${percentageChange}`)
15+
// there are 5 function calls in this file. including methods used , 2 methods = replaceAll(), methods are called under their object, replaceAll() is called under the string object.
16+
// The lines where a function call is made are:
17+
// Line 4: carPrice.replaceAll(",", "")
18+
// Line 5: priceAfterOneYear.replaceAll(",", "")
19+
// Line 7: console.log(`The percentage change is ${percentageChange}`)
2020

2121
// 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?
22-
//syntaxError: missing ) after argument list, -
23-
//the error is occurring because of a missing "," in the replaceAll() method. in line 5.
22+
//syntaxError: missing ) after argument list, -
23+
//the error is occurring because of a missing "," between arguments in the replaceAll() method. in line 5.
2424

2525
// c) Identify all the lines that are variable reassignment statements
26-
// The lines that are variable reassignment statements are:
27-
// Line 4: carPrice = Number(carPrice.replaceAll(",", ""));
28-
// Line 5: priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
26+
// The lines that are variable reassignment statements are:
27+
// Line 4: carPrice = Number(carPrice.replaceAll(",", ""));
28+
// Line 5: priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
2929

3030
// d) Identify all the lines that are variable declarations
31-
// The lines that are variable declarations are:
32-
// Line 1: let carPrice = "10,000";
33-
// Line 2: let priceAfterOneYear = "8,543";
34-
// Line 7: const priceDifference = carPrice - priceAfterOneYear;
35-
// Line 8: const percentageChange = (priceDifference / carPrice) * 100;
31+
// The lines that are variable declarations are:
32+
// Line 1: let carPrice = "10,000";
33+
// Line 2: let priceAfterOneYear = "8,543";
34+
// Line 7: const priceDifference = carPrice - priceAfterOneYear;
35+
// Line 8: const percentageChange = (priceDifference / carPrice) * 100;
3636

3737
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
38-
// The expression Number(carPrice.replaceAll(",","")) is converting the string value of carPrice into a number by removing the comma from the string.
38+
// The expression Number(carPrice.replaceAll(",","")) is converting the string value of carPrice into a number by removing the comma from the string.

0 commit comments

Comments
 (0)