Experimenting with for loops

I wanted to test my understanding of for loops on my own in JS Fiddle and for the most part my function is acting as intended.

I want my for loop to increment i for however many times specified when the function is called, which it is doing. In my for loop I added the line nameArray.shift; hoping that after the first item in the array was moved to new array it would be shifted out of name array. Right now, the function is just adding name 1 several times. What am I doing wrong?

It is because of this line.
newArray.push(nameArray[0]);

Also be careful of the syntax with shift. It should be written like this arr.shift()

Shouldn’t using .shift() get rid of whatever is in position 1 of the array? That would make the next item position 0, no?

adding the () to shift fixed everything, thanks.

1 Like