Loop the loop with loop


#1

Continuing the discussion from Loop the loop with loop "break if 30":

I've tried all the things that everyone is putting up here... I've cut and pasted all the different suggestions and to no avail. I still get "Oops, try again. It looks like you didn't print out the string 'Ruby!' 30 times."
Just like the last guy it still produces the same result....
Does anybody know why??


#2

This will work, provided m is defined

m = "Ruby!"
30.times { print "#{m}"}

#3

Alright, let's take a few steps to see if we can find what went wrong.

1) Post the code you used using back-tick notation (underneath the tilde " ~ "):

```
30.times{ print "Ruby!" } #your code here!
```

It'll look like this

30.times{ print "Ruby!" } #your code here!

2) Post the error, and Instructions for the forum. This pops up when you hit "Save & Submit Code"


#4

m = 1
loop do
m += 1
print "Ruby! "
break if m == 30
end


#5

Oops, try again. It looks like you didn't print out the string 'Ruby!' 30 times.


#6

Try starting with m = 0.


#7

m = "Ruby! "
loop do
30.times{ print "#{m}"}
break if m > 30
end
every one of these work in the display window... but I still get the "Oops, try again. It looks like you didn't print out the string 'Ruby!' 30 times."


#8

That's because you are printing the string, 'Ruby! ' with a trailing space. Take out the space.

There is a big issue here with putting a loop in a loop like that. All that is needed is either one loop or the other.

m = 'Ruby!'
30.times {print "#{m}"}

which is a refactoring of,

m = "Ruby!"
i = 1
loop do
    print "#{m}"
    i += 1
    break if i > 30
end

#10