Suggested Solution for "Iterate with javascript while loops"

Link to challenge:

Suggested Solution:

Solution
// Setup
var myArray = [];

// Only change code below this line
var i = 0;
while(i <= 5) {
  myArray.unshift(i);
  i++;
}

I didn’t see one in the forums using unshift

Hey Ankur,

nice to meet you! :wave:

unshift adds a new item to the first index,
meaning every other array index needs an update.
This can become very slow.

So theoretically this is a viable solution,
practically you don’t want to teach this as best practice.

Ah, that makes sense. Thanks for the clear and succinct explanation!

Nice to meet you too

1 Like