Ruby Ruby Loops Ruby Loops The Ruby Loop

Richard Poirier
PRO
Richard Poirier
Pro Student 2,930 Points

A few questions on loops and pushing info into an array

I have tried this various different ways and I am not sure why the loop isn't working. The video is always so straight forward (i.e. easy) and sometimes you get to the code challenge and it is way more complex. It is either giving errors saying code is running to long (which I assume isnt terminating) or it just says "bummer...try again" which isnt helpful. I think I am a little confused on a few points.

I assume that when you put number = 0, this variable is defined as an integer (assume it is a kernel (fixnum)). However, I also assume you have to set up a function to add 1 to the number variable. So I called this sum. Wouldn't this be what you would add to the array? Sometimes the answers they are looking for are so specific it is hard to know if I am WAY off or just not what challenge wants you to specifically answer.

Because it is an array...don't you use the numbers.push(answer) to add sum value to array? I wasn't sure if you if the program is thinking my sum variable is a string so made a new variable called answer to convert "sum" into an integer. I wasn't sure how else to do this in the code. Additionally, wouldn't you use "#{}" since new number is being added and this is changing?"

There are some random questions I guess I still had from array/hash section that weren't super clear to me. Thanks Treehouse community...you all are the best! = )

-Richard

PS - I think my code may be way overthought by the time I put it down...I was getting more and more complicated trying to figure it out...not sure where I am going wrong

PPS - Does anyone have a good ruby interface that can be loaded onto your computer to work with Ruby in an interface line on workspace?

loop.rb
numbers = []

number = 0

sum = number + 1
answer = sum.to_i

loop do
  numbers.push("#{answer}")
  if numbers == 3
    break
  end 
end

1 Answer

Jennifer Nordell
STAFF
Jennifer Nordell
Treehouse Staff

Yes, you're over thinking this, I think. You've added variables that they haven't asked for and aren't really needed. And even though your answer variable isn't needed, I feel obligated to point out that there would be no point in trying to convert it to an integer. It's already an integer. The number variable is an integer and sum adds two integers (0 + 1) which results in an integer. Yes, you can use push to push an item into an array. However, there's no need to push in the string version of the "answer". It simply wants you to push in an integer. At the end your numbers array should look like this [0, 1, 2] and not ["0", "1"."2"] which is where I think you were trying to go with your code. Take a look at my solution:

numbers = []

number = 0

# write your loop here
loop do
  numbers.push(number)
    if numbers.length == 3
      break
    end
  number = number + 1
end
Estevan Montoya
Estevan Montoya
Pro Student 22,408 Points

This code works correct other than the if statement needs to "== 2" rather than "== 3" because of the index starting at 0.