Print all even numbers from array?

Print all even numbers from array?
0

#1

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;
  }
}

#2

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


#3

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.


#4
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.


#5

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


#6

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.


#7

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


#8

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…


#9

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.


#10

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;
}

#11

Test it out and see.


#12

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


#13

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. :slight_smile:


#15

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)

#16

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.


#17

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;
}

#18

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);

#19

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]


#20

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


#21