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

Vineet Kapoor
Vineet Kapoor
3,955 Points

Why is the following code wrong for challenge task 1 of 1 of Ruby Loops?

Using the loop construct, add the current value of number to the numbers array. Inside of the loop, add 1 to the number variable. If the numbers array has more than 3 items, use the break keyword to exit the loop.

numbers = [] number = 0

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

loop.rb
numbers = []

number = 0
numbers << number
loop do
  if numbers.length > 3
    break
  else
    number += 1
  end
  numbers << number
end
# write your loop here

4 Answers

I think your code is correct but the challenge question is worded incorrectly. If you break out of the loop when the numbers array has more than three items, you get a hint that the numbers array does not contain three items. However, using your code and setting the break

if numbers.length > 2 

passes the challenge. So it seems the question is asking you to create a 4 item array and the grader is checking for a 3 item array.

David Clausen
David Clausen
11,403 Points

Yes! I've actually brought this up and even a moderator here couldn't comprehend the question and the evaluation are checking for two different things. I submitted a ticket though. This is going to confuse a lot of new programmers with the wording and check being different. I'd highly recommend submitting a ticket Erika since you understand the issue to so we can hopefully get it resolved for others.

Thanks!

David,

Thanks, that's a good idea. I've sent this question to the Treehouse Support Team for review.

Jason Seifer,

Would you please explain this seemingly simple Ruby Loops Challenge. There is ongoing confusion about the correct answer. I emailed the Treehouse Support team but was directed back to the Community Forum.

This is the issue: The challenge question asks you to exit a loop if the numbers array has more than 3 items. However, you are only able to pass this challenge if you exit the loop as soon as the numbers array has more than 2 items. To me and some other students on the forum, these are two different things. What are we missing?

you all are using way more code then is needed

loop do 
  number += 1
  numbers.push(number)
  if numbers.length > 2
    break
  end
end

this is the code for the loop.

hope this helps and happy coding

David Clausen
David Clausen
11,403 Points

Eric, count =/= index. A array with an three items [1,2,3] had a length of 3, its index would be 0,1,2.

The question is asking for number of items, not index. It's askng to stop after having a total of More than 3 objects. If you have more than 3 you will have to stop at four.

If you inspect you array you will see you break at exactly 3.

Is having three items more than 3?

This isn't about more work vs not...this is simply wrong and its confusing people. You now have index and count/length mixed up. This is quite annoying and its vital to understand the difference of the two.

Length/count is the quantity, I'd you have zero items you have 0 length. Index is the position in a collection items are stored, this starts at zero.

A array that has four items you will need to use index of 3 to access the last item and 0 to access the first and -1 to access the last element.

Hope this clears up what the crux of the issue is.