Why does TypeError: arr[i] is undefined appear?

function filteredArray(arr, elem) {
  // Only change code below this line
for (let i=0; i<arr.length;i++) {
  for(let j=0;j<3;j++){
if (arr[i][j]==elem) arr.splice(i,1);
  // Only change code above this line
  return arr;

console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));

Have you declared the variable arr yet. Can you provide a link to the challenge?

but it’s a parameter, not just a undeclared variable; plus, in a previous challenge I’ve had the same syntax but no error appeared - Basic Data Structures: Add Items Using splice()

Can you go to the challenge and press the button get help, and ask for help, so it will link the challenge here, because I got a different type of challenge

you are iterating over arr and changing it at the same time.
This gives unexpected behaviours.
One of which is that some elements of arr are not going to be tested.
An other is that at one point while doing this check if (arr[i][j]==elem) you will find that arr[i] is undefined



I see a problem here

You need to define where arr.splice( ) should start. You can’t start with undefined i.

I am not sure what you wanted to achieve, but the following code might solve your problem. :slight_smile:

  if (arr[i][j]==elem) {
    return arr.splice(1,2);

return arr;

However, maybe there is a better way to solve your question.

1 Like