0

So I have this code:

var myArray = [];

var value = 5;

while (myArray != [5, 4, 3, 2, 1, 0]) {

  myArray.push(value)

  value--;

  console.log(myArray);

}

when I look at the console, it goes on for an infinite loop like so..

[ 5 ]
[ 5, 4 ]
[ 5, 4, 3 ]
[ 5, 4, 3, 2 ]
[ 5, 4, 3, 2, 1 ]
[ 5, 4, 3, 2, 1, 0 ]
[ 5, 4, 3, 2, 1, 0, -1 ]
[ 5, 4, 3, 2, 1, 0, -1, -2 ]
[ 5, 4, 3, 2, 1, 0, -1, -2, -3 ]

..........

Why doesn't it stop at [5,4,3,2,1,0] ? myArray = that at a point and the for loop should stop no?

Sorry for the noob question.

EternalHour
  • 8,308
  • 6
  • 38
  • 57
  • 3
    It doesn't work, because two arrays with the same content do not have equal reference in the system - try this in the console `[] === []`. Just use `value > -1` as the condition. – Ori Drori Apr 17 '20 at 23:07
  • 1
    Thank you @Ori Drori, I didnt know arrays could not equal each other.. When I try that in the console, I see "false". Yes Value > -1 works perfectly. I appreciate the explanation! – Christopher Barreto Apr 17 '20 at 23:09
  • try `if(value >= 0) {` – Yukulélé Apr 17 '20 at 23:20

3 Answers3

2

JavaScript does not provide built-in support for structural-equality of Arrays, but it's straightforward to implement a comparator:

function arraysEqual(a, b, orderSensitive = true) {
  // Function from https://stackoverflow.com/a/16436975/159145
  // But modified to add the `orderSensitive` option.

  if (a === b) return true;
  if (a == null || b == null) return false;
  if (a.length != b.length) return false;

  if (!orderSensitive) {
    a = Array.from(a).sort();
    b = Array.from(b).sort();
  }

  for (var i = 0; i < a.length; ++i) {
    if (a[i] !== b[i]) return false;
  }
  return true;
}

function yourCode() {
  var myArray = [];
  var value = 5;
  const finalArray = [5, 4, 3, 2, 1, 0];

  while (!arraysEqual(myArray,finalArray)) {
    myArray.push(value)
    value--;
    console.log(myArray);
  }
}
Yukulélé
  • 15,644
  • 10
  • 70
  • 94
Dai
  • 141,631
  • 28
  • 261
  • 374
0

you can use the following way to fix the issue if index and values of both arrays are different

while(sortArray(myArray).toString() !== sortArray([5, 4, 3, 2, 1, 0]).toString()) {
// your code
}

sortArray(array) {
  return array.filter((a, b) => a-b)
}

If you are sure the array index and values for both arrays are same you can use

while(myArray.toString() !== [5, 4, 3, 2, 1, 0].toString()) {
// your code
}
Karthik
  • 1,088
  • 6
  • 17
-1

@Christopher Barreto and welcome to StackOverflow. Good luck with your interesting question.

although @Dai right with his full answer there is much simpler built in a way to convert array to string and then compare them.

That will work for you:

var myArray = [];

var value = 5;

while (myArray.toString() != [5, 4, 3, 2, 1, 0].toString()) {

  myArray.push(value)

  value--;

  console.log(myArray);

}

Or this if you prefer:

while ((''+myArray) != ('' + [5, 4, 3, 2, 1, 0]))
pery mimon
  • 7,713
  • 6
  • 52
  • 57
  • This is awesome, Thank you Pery. its literally converting the array into a string then comparing the strings. I'll have to add this one to my notes. – Christopher Barreto Apr 18 '20 at 00:01
  • String conversion is very inefficient though - I strongly recommend _against_ using this in production code, even more-so because the string representation of `[5, 4, 3, 2, 1, 0]` will be regenerated **every** time the `while` loop repeats itself. – Dai Apr 18 '20 at 00:21
  • Not every code is production code. know how to write quick code is an essential skill. also, this approach used under code in `c `or ` asm` for the conversion. so it much faster then all the `if` in the first answer. – pery mimon Apr 18 '20 at 13:41