0

I'm trying to loop through the columns, using a while loop but every time I'm getting an error and I was wondering if you guys can tell me what I did wrong. the code is this:

var arr = [
    [['cat', 'fish'],['dog', 'meat']],
    [['cat', 'toy'],['dog', 'bone']],
    [['cat', 'fish'],['dog', 'bone']]
];

var position = 0;
//can I do this in stead?
//while(arr.length > position){

while(true){
  var arrEnd = true;
  for (var k = 0; k < arr.length; k++) {
    if(arr.length > position){
      arrEnd = false;
    }
  }

  if(arrEnd){break;}

  for(var i =0; i < arr.length;i++){
    for(var j =0; j < arr.length;j++){
      if(i != j && arr[i][position][1] == arr[j][position][1]){
        console.log(arr[i][position]+'===='+arr[j][position]);
      }
    }
  }
  position++;
}
//Expected:
// "cat,fish====cat,fish" "cat,fish====cat,fish"
// "dog, bone====dog,bone" "dog, bone====dog,bone"
//Error getting: arr[i][position] is undefined

What is wrong? I can't see the issue.

6
  • Why do you go through the same array 3 times? Commented Aug 10, 2016 at 5:57
  • can u tell us what you want to accomplish or what is your desired result at the end? Commented Aug 10, 2016 at 5:58
  • @GopinathShiva I add what I was expecting. Commented Aug 10, 2016 at 5:58
  • You can check yourself what went wrong by visualizing your javascript code one step after another over here - pythontutor.com/visualize.html#mode=edit Commented Aug 10, 2016 at 5:59
  • @smnbbrv the first is to go out of the loop. Commented Aug 10, 2016 at 5:59

2 Answers 2

1

You have a three dimensional array. You are originally using position against the first index of the array, then later, where the error occurs, you use position to reference the second index. Since the first dimension is longer than the second dimension, you eventually get an error as a result of the value at the requested index not being defined.

Assuming that the inner arrays are of equal length at each level, this works:

var arr = [
    [['cat', 'fish'],['dog', 'meat']],
    [['cat', 'toy'],['dog', 'bone']],
    [['cat', 'fish'],['dog', 'bone']]
];

var position = 0;
while(true){
  var arrEnd = true;
  for (var k = 0; k < arr[0].length; k++) {
    if(arr[0].length > position){
      arrEnd = false;
    }
  }

  if(arrEnd){break;}

  for(var i =0; i < arr.length;i++){
    for(var j =0; j < arr.length;j++){
      if(i != j && arr[i][position][1] == arr[j][position][1]){
        console.log(arr[i][position]+'===='+arr[j][position]);
      }
    }
  }
  position++;
}

Note the additional [0]s in the first for loop.

Though, really, the code should be rearranged to be more clear as to what's going on so as to make issues like this easier to see:

var arr = [
    [['cat', 'fish'],['dog', 'meat']],
    [['cat', 'toy'],['dog', 'bone']],
    [['cat', 'fish'],['dog', 'bone']]
];

for(var i=0; i < arr.length-1; i++){
  for(var j=i+1; j < arr.length; j++){
    for(var k=0; k < arr[i].length; k++){
      if(arr[i][k][1] == arr[j][k][1]) {
        console.log(arr[i][k]+'===='+arr[j][k]);
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your problem is with while (true) and position++ outside of for. Eventually, position will be greater than 1, causing the exact error you're receiving.

One more thing to note, your arr has length of 3, while max position number (second dimension index) would be 1 (since you have 2 arrays in your subarray).

Instead of your nested for loop, try using Array.prototype.forEach.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.