Ruby Ruby Loops Ruby Iteration The For Loop

Kerem Kazan
Kerem Kazan
Full Stack JavaScript Techdegree Student 5,280 Points

for loop not running

it says 'the for loop was not used' although it's there. what am i missing?

for.rb
animals = ["dog", "cat", "horse", "goat"]

for i in 0..3 do
  puts animals[i]
end

1 Answer

Chris Shaw
Chris Shaw
26,624 Points

Hi Kerem,

At the moment your for loop isn't referencing the animals array but instead you have 0..3, instead this should be referencing animals so the for loop can iterate over each value.

One other thing is you're using the variable i within square brackets which isn't needed as ruby will automatically assign each array value to i therefore it would make far more sense to call it animal instead and use that in our puts statement.

With that said you would end up with something like the following which is what the challenge is expecting.

for animal in animals do
  puts animal
end

Happy coding!