Try It Out! Confusion


#1



So, I've been having some troubles with lesson twelve. I needed some help earlier as to how I make the code, but now I need some help with the code itself. Below you will find the link to the lesson, as well as my current strings I am using.


The thing is, my code WORKS, but not how I want it to. It simply just prints the array and doesn't bother with multiplying the numbers. So, can I please get some help as to how to correctly code it to multiply the numbers in the array by 2?


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

# Add your code below!
odds.each do |x|
    print {x * 2}
end


#2

Most things behave some way, yes. (works is a bad description of most things, since it does not describe)

Why would it print out the numbers multiplied? If you comment out the line that you think it would be doing that, does the behaviour of your program change in any way? And what can you tell from that? And in what way would you therefore need to change your code?

What do you think the variable x will refer to after doing this?

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

# Add your code below!
x = (odds.each do |x|
    print {x * 2}
end)

(Yeah, it's actually an expression and therefore has a result, and that result is what you're seeing printed by codecademy (not ruby itself, because there's no print/puts doing that))


#3

After messing around with it a bit, I found out the "{}" marks were messing with my code to seemingly exclude the multiplication portion of the code. It now works as intended, but, I did not know the "{}" actually done anything at all, nor did I know if I was supposed to put them there or not. They were in the example's code, so I assumed that I needed to put them there.

(Note to self : Find a more in depth guide about the uses of different keywords and symbols besides the glossary.)

Thanks anyways.


#4

Have a look at the examples in the instructions, you did a bit of both versions ending up being neither of the two ways you could do it.

And if you think something does nothing, then remove them. (Shouldn't have things in your code that "probably does nothing" because it's either clutter or it's doing something you're not aware of, either is bad


#5

yeah the brackets are the problem see!!!!!!!!

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

# Add your code below!

odds.each do |x|   
    print x * 2
end

#6

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