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 Loops The Ruby Loop

Andrew Walters
Andrew Walters
8,876 Points

Ruby Loop, Challenge One

Hey guys! So I'm really struggling on the first Loop Challenge in Ruby which is asking us to insert a variable into an array, and then to count that variable up (via a loop) and when you reach 3 the loop should end. I could swear that I am doing it right but I don't seem to be passing the challenge. I would either get a void value message or "You're code took too long to run". Can anyone help to explain what I'm doing wrong? Thanks!

loop.rb
numbers = []

number = 0

# write your loop here
loop do 
  numbers = [number] 
  number ++
  if numbers.length > 3
    break
  end
end

2 Answers

Kelsey Jackson
seal-mask
.a{fill-rule:evenodd;}techdegree
Kelsey Jackson
UX Design Techdegree Student 63,504 Points

Hey Andrew,

You are almost there, you just have some slight syntax errors, the code below should pass:

numbers = []

number = 0


loop do 
  numbers.push(number)
  if numbers.length >= 3
    break
  else
    number += 1

  end

end
Andrew Walters
Andrew Walters
8,876 Points

That makes sense but why would you want to break the loop before adding one? And why greater than or equal to 3? The question seems to request a break if above 3 but doesn't indicate if being equal to 3 is okay

Kelsey Jackson
seal-mask
.a{fill-rule:evenodd;}techdegree
Kelsey Jackson
UX Design Techdegree Student 63,504 Points

That break will execute only when the conditional is true, if not it will increment and run again. The wording in the question is a little misleading but >= 3 or >2 would work.

Andrew Walters
Andrew Walters
8,876 Points

Ah, okay I see. Thanks for clarifying it! Much appreciated!

Philip Bessa
Philip Bessa
5,396 Points

has more than 3 items should instead state has 3 items or more. It's not misleading, it's just wrong:

if numbers.length > 3

But after it fails, the Bummer message points to a hint (finally it helps for once):

if numbers.length >= 3