2

I'm trying to figure out why the following piece of code only pops the last 3 items off the array but not the last two.

var test = ['test1','test2','test3','test4','test5'];

console.log('length - '+test.length);

for(var k = 0; k <= test.length; k++) {
    var tests = test.pop();
    console.log(tests+' - '+k);
}

The results for the above code:

length - 5
(index):30 test5 - 0
(index):30 test4 - 1
(index):30 test3 - 2
0

5 Answers 5

3

Array.prototype.pop method modifies the original array, so of course for-loop will only visit half of the values until array is fully emptied.

What you can do is to use while loop instead:

var test = ['test1', 'test2', 'test3', 'test4', 'test5'];

console.log('length - ' + test.length);

while (test.length) {
  var tests = test.pop();
  console.log(tests);
}

Sign up to request clarification or add additional context in comments.

Comments

2

The problem is that the length of the array is changing while the loop is executing, so the test
k <= test.length is changing with each execution.

var test = ['test1','test2','test3','test4','test5'];

console.log('length - '+test.length);

for ( var k = 0, len = test.length; k < len; k++ ) {
  var tests = test.pop();
  console.log(tests+' - '+k);
}

Or with a while loop:

var test = ['test1','test2','test3','test4','test5'];

console.log('length - '+test.length);

var k = 0;
while (test.length) {
    var tests = test.pop();
    console.log(tests+' - '+(k++));
}

Comments

1

You're increasing k each time. After printing 2, k == 3 while there are still 2 elements left in the list. But since k > test.length, the loop ends.

If you really still want to keep count, increase k but don't test it:

var test = ['test1', 'test2', 'test3', 'test4', 'test5'];

console.log('length - ' + test.length);

for (var k = 0; test.length > 0; k++) {
  var tests = test.pop();
  console.log(tests + ' - ' + k);
}

Comments

1

Consider the logic of what you're doing... you're burning your candle at both ends (so to speak).

k = 0, length = 5 ('test1') at this point test5 is thrown away (that's what pop does, it drops the last element)

k = 1, length = 4 ('test2') (at this point test4 is thrown away)

and so on... after 3 iterations your array is 3 items.

I *think what you're trying to do you would accomplish with splice

while(myArray.length > 0){
   console.log(myArray[0])
   myArray.splice(0,1)

}

Comments

0

If you like to keep your code style, just added following line. This is to prevent array length re-sizing in the for loop:

var arrSize = test.length;

Your final code would look like this:

<script>
    var test = ['test1', 'test2', 'test3', 'test4', 'test5'];

    var arrSize = test.length;

    console.log('length - ' + arrSize);

    for (var k = 0; k < arrSize; k++) {
        var tests = test.pop();
        console.log(tests + ' - ' + k);
    }
</script>

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.