Loops & Iterators :Make sure to replace the ___s with the appropriate keywords!


#1


https://www.codecademy.com/en/courses/ruby-beginner-en-XYcN1/2/2?curriculum_id=5059f8619189a5000201fbcb


This is the only thing the message says:

Make sure to replace the ___s with the appropriate keywords!

Can someone explain to me where I am going wrong?


i = 20
loop do
  i -= 
  next if i / 2 != 0
  print "#{i}"
  break if i <= 0
end


#2

This will not help us determine parity of a number. The only number that will give a zero when divided by 2 is 0. We need to use modulo to test for even parity.


#3

I haven't even gotten to that point in the lessons yet. If you see the link, you'll see they don't ask me to use that yet.


#4

Oh, but they do...

Add a line to your loop before your print statement. Use the next keyword so that you skip to the next iteration if the number i is odd.


#5

What I mean is we haven't gotten to use modulo yet.


#6

Then how does one find themselves in the thick of a Ruby course without that background knowledge. Are you cherry picking modules without covering the basics of operators? Either that or you are in elementary school and haven't covered this in your maths class, yet?

This is something that we need a pencil and paper for. Your calculator will not display the remainder in long division.

12 / 5  =>  2.4

12 % 5  =>  2

Notice that 2 is 0.4 * 5.

  ______
5 ) 12 | 2
    10
   ----
     2

Answer: 12 / 5 is 2, remainder 2.


#7

I am a Director of IT/Infrastructure, I am learning Ruby and Rails by going through the courses here so I can follow along with what the programmers are talking about, and also in the long run to help out if need be.

Unless I missed the code you are talking about, I don't remember reviewing it in the chapters past. Oh, and a side note, I was told by one of the leads to go here to learn Ruby, unless you have anything else to add, as if this is not a place to learn Ruby from scratch, I'd like at least a little understanding upon why I am getting the error... because as it stands now, looking around on this forum, I don't really see a straight answer.

Because the Instructions on what to do state:

Add a line to your loop before your print statement. Use the next keyword so that you skip to the next iteration if the number i is odd.

Use the example above for help, but remember that the example above skips even numbers.

And before you say I should know this, let me explain... my first Admin job, Windows didn't have a GUI, so I'm catching up on things I should know.


#8

Ok, but how do I make it so that it figures it out each time it goes down a number and skips every odd number?


#9

Figured it out, the error wasn't in the calculation... it was right after the loop do.

i -=

needed to be i -= 1


#10

Test for oddness

  next if i % 2 != 0

This is the example given...

for i in 1..5
  next if i % 2 == 0
  print i
end

which is a test for evenness.

If you need a math refresher, check out Khan Academy. It has everything from elementary arithmetic all the way up to pre-college calculus.


#11

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