Can the counter ever be 0? Like i = 0


#1

Because this did not work:

i = 0
while i < 5
puts i
counter = counter + 1
end

(The i (counter) was originally 0 in this exercise. But the error message stated a problem with (+) for some reason.)

When I changed the 0 to a 1, it did work:

i = 1
while i < 5
puts i
counter = counter + 1
end


#2

There should not be a problem with starting at zero. Please post a link to the exercise so we can take a closer look.


#3

Here is the link to the exercise.:

https://www.codecademy.com/courses/learn-ruby/lessons/loops-iterators/exercises/danger-infinite-loops?action=resume_content_item

I’ve already moved past this lesson so now when I go back to it, it shows different code (See below. It uses i = 0 (not i = 1) and i = i + 1 (not counter = counter + 1). Maybe inconsistency was the problem; using counter instead of i.

i = 0
while i < 5
puts i
i = i + 1
end


#4

i is the left hand; counter is the right hand. Neither know what the other is doing. This last example works because there is no disconnect with the variables. i is consistent throughout.


#5

That’s what I suspected. Thank you!!


#6

You are welcome. I would leave you with the expectation of more stuff like this, which is what helps us to become better at debugging. Comes with practice. Everybody stumbles. We are never without company in thiat regard.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.