Skip to content

Commit abd38ec

Browse files
committed
2-mandatory-errors completed
1 parent 8833f3c commit abd38ec

5 files changed

Lines changed: 20 additions & 2 deletions

File tree

Sprint-2/2-mandatory-errors/0.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
This is just an instruction for the first activity - but it is just for human consumption
2-
We don't want the computer to run these 2 lines - how can we solve this problem?
2+
We don't want the computer to run these 2 lines - how can we solve this problem?
3+
4+
//We have to use the // to comment a single line
5+
/* or use /* to comment multiple lines */

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22

33
const age = 33;
44
age = age + 1;
5+
/* const means that the variable cannot be reassigned we should use let keyword instead.
6+
TypeError displayed due to that. */

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@
33

44
console.log(`I was born in ${cityOfBirth}`);
55
const cityOfBirth = "Bolton";
6+
7+
//The error we see it's a ReferenceError which tells us that we cannot access variable before initialization.
8+
9+
/*JavaScript reads the code from top to bottom, order is wrong.
10+
Task cannot be executed as variable cannot be access before being created. */

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
const cardNumber = 4533787178994213;
2-
const last4Digits = cardNumber.slice(-4);
2+
const last4Digits = cardNumber.toString().slice(-4);
3+
console.log(last4Digits);
34

45
// The last4Digits variable should store the last 4 digits of cardNumber
56
// However, the code isn't working
67
// Before running the code, make and explain a prediction about why the code won't work
78
// Then run the code and see what error it gives.
89
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
910
// Then try updating the expression last4Digits is assigned to, in order to get the correct value
11+
12+
13+
/*The code would not work because cardNumber variable stores number data type and .slice() method cannot be used on a number,
14+
if we run the code without changing data type to string we will get TypeError displayed
15+
I used .toString() method to convert cardNumber variable to a string and then used .slice(-4) to get the last 4 digits. */

Sprint-2/2-mandatory-errors/4.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
const 12HourClockTime = "8:53pm";
22
const 24hourClockTime = "20:53";
3+
4+
//A variable cannot start with a number, SyntaxError is displayed.

0 commit comments

Comments
 (0)