0

Can someone please tell me why the length of secondArrayis 5 instead of 10?

The result I am looking for is for all elements to be popped off so the secondArray has an empty. However, it seems that only half of them are being popped off, even though I have set the (condition) of the for loop to go through the entire array. Can someone point out why this is?

Please Note: I understand this is not the only/or ideal way to remove elements from an array. This is simply some practice I am doing with for loops and Array methods.

my result looks like this after executing the code:

secondArray = 1,2,3,4,5,6,7,8,9,10

secondArray = 1,2,3,4,5

var secondArray = [1,2,3,4,5,6,7,8,9,10];

document.write("secondArray = " + secondArray)

for(i = 0; i < secondArray.length;  i++){
    secondArray.pop(); 
}

document.write("<br/>"+ "secondArray = " + secondArray)
3
  • 1
    What are you expecting to happen? Because it's doing exactly what you're telling it to do. Commented Jul 17, 2016 at 19:56
  • 1
    Every time you loop secondArray.length changes because you keep popping off elements Commented Jul 17, 2016 at 19:57
  • If you want to empty out secondArray, then just do secondArray.length = 0;. Commented Jul 17, 2016 at 20:10

5 Answers 5

4

You should rewrite this as:

while (secondArray.length) secondArray.pop();

I will not address the question of why you are trying to empty an array by popping off all its elements, instead of just setting the length to 0.

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

Comments

2

secondArray.length gets calculated for each iteration. On each iteration secondArray has one item less.

To pop() all the elements save secondArray.length and use the variable:

var firstArray = [];
var secondArray = [1,2,3,4,5,6,7,8,9,10];

document.write("secondArray = " + secondArray)

var len = secondArray.length;

for(i = 0; i < len;  i++){
    secondArray.pop(); 
}

document.write("<br/>"+ "secondArray = " + secondArray)

Note

There are many other and better ways to empty an array as OP aware of already. The answer addresses the common mistake of thinking that a for loop condition is calculated only at the first iteration.

2 Comments

I seee, so what's happening is that secondArray.length which is originally 10, loses one length each time until it gets to less than 5 and the condition is no longer true??
My personal preference is for (var i=0, l = secondArray.length; i<l; i++) - initialising the variables together - to me at least - makes it clearer that they work together.
2
var firstArray = [];

var secondArray = [1,2,3,4,5,6,7,8,9,10];

console.log(secondArray)

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] //nothing change

 //first loop runs
 for(i = 0; i < secondArray.length;  i++){ 
    // removing one element and now only 9 elements left 
    // means length have changed 
    // it's is doing his job as expacted
   secondArray.pop();         
   console.log(secondArray)
 }
// first run when length was 9  [1, 2, 3, 4, 5, 6, 7, 8, 9]
// second run when length was 8  [1, 2, 3, 4, 5, 6, 7, 8]
// third run when length was 7  [1, 2, 3, 4, 5, 6, 7]
// fourth run when length was 6  [1, 2, 3, 4, 5, 6]
// fifth run when length was 5 [1, 2, 3, 4, 5]
// and here loops end because of given condition 

4 Comments

You have explained what is happening, but not how to fix it.
i don't know what do you want to achieve??
It's not me, but it would seem like he wants to pop all elements off the array.
sorry i got confused and mixed you with the person who was asking the question, yeah i know he seems to do that i didn't understand what he want to exactly do.. he seems to be find his answer from @haqen H'ghar
1

Because you are starting the loop at 0 and as long as the length of the array is less than your length the loop will continue. How every once it reaches 6 the i variable become grater than the length of the array because you are popping a value out of the array with each run through the loop.

array = 10, 9, 8, 7, 6, 5, 4

var = 0, 1, 2, 3, 4, 5, 6

It terminates when your var[6] is greater than your array[4].

Comments

1

The pop method removes the last element from an array and returns that value to the caller.

It means the result is the expected.

If you want to clear the array put the initial array length in a var to make it static, but I can see two ways to do that better.

  1. Just assign array.length to 0
  2. Assign the array to be an empty array.

Check the docs

2 Comments

ok if the pop method takes the last element from the array then why has it not removed the last element 10 times i.e the length of the second array?
Because you are poping the initial array, each means that by the fifth iteration length is 5 and the loop exits early.. Just put the initial length in a variable if you want to clear the array

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.