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.

3
  • 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. Commented Apr 17, 2020 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! Commented Apr 17, 2020 at 23:09
  • try if(value >= 0) { Commented Apr 17, 2020 at 23:20

3 Answers 3

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);
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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
}

Comments

-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]))

3 Comments

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.
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.
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.

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.