Skip to content

Commit 8e67204

Browse files
authored
Fix last4Digits assignment for cardNumber
Updated last4Digits assignment to convert cardNumber to string before slicing.
1 parent b12c3a3 commit 8e67204

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

  • Sprint-2/2-mandatory-errors

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
const cardNumber = 4533787178994213;
2-
const last4Digits = cardNumber.slice(-4);
3-
2+
const last4Digits = cardNumber.toString().slice(-4);
3+
console.log(last4Digits);
4+
//the output is 4213
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
8+
//it won't work because cardNumber is not a string
79
// Then run the code and see what error it gives.
10+
//TypeError: cardNumber.slice is not a function so a string method can't br used with it
811
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
12+
// The error is caused because card number is a number not a string and toString is a string method so its a different type of data
913
// Then try updating the expression last4Digits is assigned to, in order to get the correct value
10-
// in this case its a reference error the program is trying to access a value that wasn't declared. In this case a variable.

0 commit comments

Comments
 (0)