File tree Expand file tree Collapse file tree
Sprint-2/2-mandatory-errors Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 ;
46age = age + 1 ;
7+ console . log ( age ) ;
Original file line number Diff line number Diff line change 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 } ` ) ;
54const 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.
Original file line number Diff line number Diff line change 11const 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".
Original file line number Diff line number Diff line change 1- const 12 HourClockTime = "8:53pm" ;
2- const 24 hourClockTime = "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.
You can’t perform that action at this time.
0 commit comments