Reputation: 75
Can someone please tell me why the length of secondArray
is 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)
Upvotes: 0
Views: 13096
Reputation: 16804
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.
Upvotes: 2
Reputation: 7621
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
Upvotes: 2
Reputation:
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.
Upvotes: 4
Reputation: 1194
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.
Upvotes: 1
Reputation: 1520
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].
Upvotes: 1