Welcome to the Treehouse Community
Looking to learn something new?
Jon Thomas8,916 Points
Problem solving Ruby loop challenge
I can't seem to solve the loop challenge for Ruby. I've attached my code. What am I doing wrong here?
numbers =  number = 0 # write your loop here loop do numbers.push(number) number += 1 if numbers.length > 3 break end end
Kevin Mulhern20,374 Points
Hey Jon, the if statement will break the loop when there are more than three elements in the array, so it will break when 4 elements are in the array. To pass the challenge you need to break when there are exactly three elements in the array so all you have to do is change your if statement so that it breaks the loop when the array length is greater than or equal to 3. The code below will make it work.
if numbers.length >= 3
Try adding an else to your if statement:
numbers =  number = 0 # write your loop here loop do numbers.push(number) number += 1 if numbers.length > 3 break else end end