ruby - Print only even numbers from an array -
i'm working way through ruby tutorial. 1 of questions provides array. have to:
"write loop puts values of
my_array
. (bonus points if use one-lineif
!)"
my answer passes test shows syntax error, answer if statement below. can explain why syntax error?
my_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] if my_array.each |x| % 2 == 0 puts x
you're missing end
statement do
, left operand modulus. try this:
my_array = (1..10).to_a my_array.each { |x| puts x if x.even? }
Comments
Post a Comment