Skip to content

Commit 4a0c07f

Browse files
committed
Complete Sprint 2 error exercises
1 parent c4b72fd commit 4a0c07f

4 files changed

Lines changed: 30 additions & 13 deletions

File tree

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
// trying to create an age variable and then reassign the value by 1
1+
// The error occurs because age is declared with const, so its value cannot be reassigned.
2+
// The next line tries to assign a new value to age, which causes a TypeError.
3+
// Using let fixes the error because let allows the variable to be reassigned.
24

3-
const age = 33;
5+
let age = 33;
46
age = age + 1;
7+
console.log(age);

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
// Currently trying to print the string "I was born in Bolton" but it isn't working...
22
// what's the error ?
33

4-
console.log(`I was born in ${cityOfBirth}`);
54
const cityOfBirth = "Bolton";
5+
console.log(`I was born in ${cityOfBirth}`);
6+
7+
// The error occurs because cityOfBirth is accessed before it has been initialized.
8+
// Variables declared with const cannot be accessed before their declaration is executed.
9+
// This causes a ReferenceError because the variable is in the Temporal Dead Zone (TDZ).
10+
// Moving the declaration before console.log fixes the error because cityOfBirth has a value before it is used.

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
const cardNumber = 4533787178994213;
2-
const last4Digits = cardNumber.slice(-4);
3-
4-
// The last4Digits variable should store the last 4 digits of cardNumber
5-
// However, the code isn't working
6-
// Before running the code, make and explain a prediction about why the code won't work
7-
// Then run the code and see what error it gives.
8-
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
9-
// Then try updating the expression last4Digits is assigned to, in order to get the correct value
2+
const last4Digits = cardNumber.toString().slice(-4);
3+
4+
console.log(last4Digits);
5+
6+
// Prediction: the code will not work because cardNumber is a number,
7+
// and the slice() method cannot be used directly on numbers.
8+
9+
// Running the original code gives a TypeError because cardNumber.slice is not a function.
10+
// This happens because slice() is available for strings and arrays, but not for numbers.
11+
12+
// Converting cardNumber to a string first allows slice(-4) to return the last four characters.
13+
// Therefore, last4Digits stores "4213".

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1-
const 12HourClockTime = "8:53pm";
2-
const 24hourClockTime = "20:53";
1+
const twelveHourClockTime = "8:53pm";
2+
const twentyFourHourClockTime = "20:53";
3+
4+
// The error occurs because JavaScript variable names cannot start with a number.
5+
// Both variable names begin with digits, so JavaScript cannot parse them as valid identifiers
6+
// and throws a SyntaxError.
7+
// Renaming the variables so they start with letters fixes the error.

0 commit comments

Comments
 (0)