Need help with iterating over an array to only pull the even numbers out of an array


#1

Question:

-Create an array called numbers containing the integers 1 - 10 and assign it to a variable.
-Create an empty array called even_numbers.
-Create a method that iterates over the array. Place all even numbers in the array even_numbers.
-Print the array even_numbers

My answer so far:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = [ ]
numbers.each { |even_numbers| puts 2,4,6,8,10 }
puts even_numbers

Replace this line with your code.


#2

@cleon0114
You will have to use the so-called modulo function
http://stackoverflow.com/questions/3517238/what-does-the-operator-do-in-ruby-in-n-2/3517240

9%3 You read it as nine modulo three is zero, as 9 divided by 3 has NO rest-value.
22%6 You read 22 modulo six is 4, as 22 divided by 6 will leave you with rest-value 4
10%5 You read 10 nodulo five is zero, as 10 divided by 5 will have NO rest-value

Now if you would use
aNumber %2 === 0 and this would evaluate to true
than you have an =even= number.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.