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

David Clausen
David Clausen
11,403 Points

"If the numbers array has more than 3 items" Issue

The question ask to break a loop when the array numbers has more than 3 items. BUT the quiz when checking checks to see if it has exactly 3 items when it breaks.

Here is a code you can paste in the quiz to show its not working properly:

numbers = []

number = 0

# write your loop here
puts "If the numbers array has more than 3 items, use the break keyword to exit the loop"
loop do
  numbers.push(number)
  number += 1

  #If MORE than 3
  if numbers.length > 3 #change to 2 to pass quiz
    puts "I have: #{numbers.length} items, which is more than 3"
    puts "Here is my array: #{numbers.inspect}\n This is more than 3 items, which would be 4 or more."
    puts "This should pass the quiz parameters"
    break
  elsif numbers.length == 3
puts ("I have: #{numbers.count} items, exactly 3 not more than 3")
  else
puts ("I have: #{numbers.length} items, not more than 3")
  end
end

I have submitted a ticket last week and wanted to check up. Since its not fix quite yet I wanna make sure people can find a answer to their problem. This error can be confusing during your learning process, but its an honest mistake it seems.

1 Answer

Tim Knight
Tim Knight
28,888 Points

David,

Consider this:

numbers = []

number = 0

loop do
  number += 1
  numbers << number
  break if numbers.length >= 3
end

Because the break doesn't happen until after you've already added the number to the array it's probably best to check if it is greater than or equal to 3... since you could be adding the third item to the array, which wouldn't be greater until it loops again and than at that point you'd have four items. If that makes sense.

David Clausen
David Clausen
11,403 Points

The question says to break after MORE than three items not greater than or equal. It didn't say to break before adding, it said to break when the array has more than three. Your example the array breaks when it has three items in it.

That doesn't meet the requirements "If the numbers array has more than 3 items" The key words are array HAS (contains) MORE THAN (greater than) three items ( more than 3 is 4 and more).

Please re-read the question cause its already confusing for new people to code the problem as stated and get it wrong, then they have to play around until the array has 3 or more.

Has 3 or more is different from has more than 3.

Tim Knight
Tim Knight
28,888 Points

Well as you mentioned, you do have a support ticket in on the issue so I imagine Treehouse directly would be able to provide their explanation or a correction to how the question is being asked. My explanation was just from my experience answering the question personally when I took the course. I look forward to hearing how things go with Treehouse support.

David Clausen
David Clausen
11,403 Points

Of course I appreciate the help! I also provided the way to pass the quiz too. Like I said, just want to inform others who look to the forum or click the "Get Help" in order to hopefully alleviate any problems or confusion with their logic.

Thanks again for trying to help, it always nice.