Iterator loop


#1

Hi everybody!!

What is wrong with my code?

accountant = 0
loop do
accountant += 1
print "Ruby"
break if accountant > 30
end

I need print "Ruby" 30 times but with iterator loop. When I run the code it tells me that it was not written 30 times.
Thanks for help me :slight_smile:


#2

That begs the question, was it? Have you counted? What was it?
The problem could be that it's saying so despite being the right amount of prints, or it could be that the amount of prints isn't right. Which is it? What's stopping you from solving this problem?


#3

it tells me that he did not write 30 times even though it did


#4

I'm going to interpret that as that you counted 30

I count 31

The way I counted was by running your code, running the output through another program that counts characters. It got 124 characters, which is 4 * 31

Alternatively, splitting up the output in groups of 5 or so in a text editor makes it easy to count manually:

RubyRubyRubyRuby
RubyRubyRubyRuby
RubyRubyRubyRuby
RubyRubyRubyRuby
RubyRubyRubyRuby
RubyRubyRubyRuby
RubyRubyRubyRuby
RubyRubyRuby

7 * 4 + 3 = 31

Yet another method is to change the code to print out just a few, like 2, and see if it really prints that amount.

One can also read the code and reason about it, perhaps execute it in your head and see if that produces the right amount (again, one would want to use a smaller number than 30) .. This has the advantage of that you'll probably see what it's doing differently from what you expect, assuming that you read/execute it correctly


#5

I am confused because i don't understand where is the trouble. this is with "break if account > 30"

Ruby! Ruby! Ruby! Ruby! Ruby! Ruby! Ruby! Ruby! Ruby! Ruby! Ruby! Ruby! Ruby! Ruby! Ruby! Ruby! Ruby! Ruby! Ruby! Ruby! Ruby! Ruby! Ruby! Ruby! Ruby! Ruby! Ruby! Ruby! Ruby! Ruby!

the resul is 30 but the program tell that is not 30. :confused:


#6

Now you've added spaces and exclamation marks, which is it supposed to be?


#7

yes but the result is the same with or without spaces and exclamation marks.


#8

Well, find out which one it's supposed to be and then make sure it's the right amount. Earlier you had 31 Ruby's which is one too many - since then you've changed two things, the number of times it is repeated AND what each one is. Try changing just one of them, and preferable check which one the instructions is asking for


#9

Let's have a look at the instructions shall we?

Use the .times iterator to print "Ruby!" to the console 30 times. Hit Save & Submit Code to see the majesty of your code and complete this lesson!

It's with exclamation mark, and without space in the string.
It also asks you to use the .times method, which you're not doing in the code you posted.


#10

I erased the space and the string is ok. Thanks for your help :slight_smile:


#11

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