# Print all even numbers from array?

Having a little bit of trouble understanding why this won’t work…

``````function question0(array){
let arr = [];
for (let i = 0; i < array.length; i++){
if (i % 2 ===0){
arr.push(array[i]);
}
return arr;
}
}
``````

You’re pushing every second value in the array, not every even numbered value

``````> question0([1,3,1,3,1,3])
[3,3,3]
``````

Edit also what @ArielLeslie said

As soon as a `return` statement is reached, function execution ends. Your `return` statement is inside your `for` loop, so only the first value of `array` is checked before the function returns.

2 Likes
``````function question0(array){
let arr = [];
for (let i = 0; i < array.length; i++){
if (i % 2 === 0){
arr.push(array[i]);
}
}
return arr;
}

``````

Yeah, fixed that, but still stuck here.

See my answer, you aren’t checking what the values in the array are

I don’t understand what you are trying to tell me. I mean, I see what you typed, but how the hell is it printing every second? I’m iterating through the array.

Your function is pushing elements of array into arr which have even index values and not even values of the elements.

I could’ve swore this code was right and I’m stuck. I don’t understand how it doesn’t run. I’m iterating through the array and checking if the index is divisible by 2. If it is, push it into the new array…

That is the problem. Why do you care if the index is even or not? You need to check the value of the element to see if it is divisible by 2 and not the index.

This can’t be it…this is it?

``````function question0(array){
let arr = [];
for (let i = 0; i < array.length; i++){
if (array[i] % 2 === 0){
arr.push(array[i]);
}
}
return arr;
}
``````
1 Like

Test it out and see.

Seems to be. Ugh, this is what I had first, too! I just switched up other lines previously. Thanks

Get a pen and a piece of paper. write your logic of the function and try to do by pen and paper what your function does. keep an eye on values and see where your code is broken. just think logical. @RandellDawson and @DanCouper are pointing you the right way. you just have to see it.

Still got a bit of a problem. It is telling me this in the terminal:

1. main question0:
AssertionError: expected 0 to equal 3
at Context.it (test/main.js:18:12)

I would need to know what input is being passed to the function to be able to help. Also, I would need to know the exact challenge instructions.

Well, this entire section is wanting me to test the results in the terminal and commit to Github.

This is all the directions I get:

``````// Write a for loop that will print only even numbers from an array:
// For example: question0([ 5, 20, 11, 42, 2, 19 ]) >> [ 20, 42, 2 ]

function question0(array){
let arr = [];
for (let i = 0; i < array.length; i++){
if (array[i] % 2 === 0){
arr.push(array[i]);
}
}
return arr;
}
``````

So you can see what array is being passed in, put the following console.log statement on the first and second lines of the function and then look at the browser’s console to see what is being displayed. Post what you see displayed in the console.

``````console.log('incoming array is');
console.log(array);
``````

incoming array is
VM49:3 (6) [5, 20, 11, 42, 2, 19]0: 51: 202: 113: 424: 25: 19length: 6__proto__: Array(0)
(3) [20, 42, 2]

Can you take a screen shot of the browser’s console instead?