10 / 12


#1

now i got an error in the interpreter saying

(ruby):2: syntax error, unexpected tIDENTIFIER, expecting keyword_then or ';' or '\n'
my_array.each { |x| if x % 2 == 0 puts x}
                                      ^

my code was

my_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

my_array.each { |x| if x % 2 == 0 puts x}

but it still let me pass that lesson. Was wondering if someone can confirm if this looks correct. if not please say something.


#2

you forget how to use if method


#3

No he didn't exactly, but if you look at the code:


There are two things missing: then and end
-All together, it could look like this, it worked for me without error

my_array.each { |x| if x % 2 == 0 then puts x end}

#4

my code:

my_array.each { |x| puts x if x%2==0}

is this ok?


#5

After entering your code, an error message appears saying:
(ruby):2: syntax error, unexpected $end, expecting '}'


But when I enter my code in the reply, the list is written out in the top right:
-It's kind of blurry when you zoom in, sorry


#6

I think wolflash is correct. Another way would be:

my_array.each do
    |x| puts x if x % 2 == 0
end

#7

I had the same error till I added spaces between the x and % and 2

my_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

my_array.each { |x| puts x if x % 2 == 0}


#8

The way I did it:

my_array.each {|a| puts a if a.even?}

#9

my_array.each { |v| puts v if v % 2 == 0}

This works