Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Loops Ruby Iteration The For Loop

Kerem Kazan
seal-mask
.a{fill-rule:evenodd;}techdegree
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,676 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!