My experience with "Loops & Iterators: Try It Out!"

I’m putting this here in hopes that other people can better understand whats going on with this lesson. If anyone spots a mistake, please point it out and back it up with clear explanation :slightly_smiling:

After scouring the forums for help on why my code isn’t working, it occurred to me that maybe I was doing exactly as ruby taught. Why wasn’t it working?

My original code

odds = [1,2,3,4,5]

odds.each do |x|
x *= 2
print “#{x}”
end

Do you see any flaws in the original coding? You shouldn’t, but before I continue, I’ll touch up on some things for others here seeking help. Skip to the bottom If you are familiar enough with the following information.

*+=, -=, =, /= are known as “assignment operators.”
x *= 2 can be written as x = x * 2. These both mean the same thing.
(or at least has been explained to me this way)

| x | inside of the | | you can put anything you wish. Letters, numbers, words etc. I chose x just to keep it simple.
(someone please tell me what the symbols | | are called, not referring to “or or”)

odds = [1,2,3,4,5] is an array. You can call this array anything you wish.
name_of_array = [values of this array, separate, each, value, with, 1comma]

You can have as many variables in your array as you wish.

odds.each do |x| is the syntax for the .each iterator.
name_of_array . each do | variable you chose |

do | | is telling what to “do” to whatever array you chose to insert. In this case the thing it’s supposed to be doing is taking .each of the variables 1, 2, 3, 4, 5 and doing whatever I defined |x| as.

I defined x as *x = 2
In English, it’s basically saying "for each of the listed numbers in the array called ‘odds’, do this math.
(I’m just a beginner at coding, but I suspect that do |x| isn’t limited to assignment operators)

print “#{x}” is called “string interpolation.” Whatever assigned value you put will appear inside #{ }. So far we’ve just been using puts, and print.
So if I said x = sharknado and then later typed “#{x}” the console would read “sharknado” .
you can place this or other string interpolations more than once, or in between other things inside of the string, for example:

print “Holy #{x}! A giant #{y} is fighting #{z}!”
This would say “Holy sharknado! A giant gorilla is fighting Godzilla!”
I added y and z to make things more interesting.

And of course, when you begin a block, you must end the block, so it knows when to end.

There is a second method besides do//end : It works if you replace do with { and end with }
(could someone confirm this is correct?)

TL;DR
Now that you have gone through this information, and the teachings of ruby, you STILL can’t seem to find what’s wrong! Don’t worry, your code is correct, but the problem is with the exercise. Ruby will only accept the end result if your array = [1, 3, 5, 7, 9] and *x = 2 OR x = x * 2

I hope this is useful in teaching newcomers like myself. If this helps, please make it known! :slightly_smiling:

2 Likes

I do differently but it still won’t work:

odds = [1, 2, 3, 4, 5]

odds.each do |placeholder|
    odds = placeholder * 2
    print odds
end

Please help me and loingelf!
:scream::scream::scream::scream::scream::scream::scream::scream::scream::scream::scream::scream::scream::scream::scream::scream::scream::scream::scream::scream:

The problem here seems to be that the original odds array in this exercise is supposed to be

odds = [1,3,5,7,9]

but in your case, it’s

odds = [1, 2, 3, 4, 5]

If you make this change, then your code should work.