9.9 Next Method


#1

Why am I not able to print just odd numbers. I don't understand what I am supposed to do or how to get it there. I get that the number is being divided and then the remainder if equal to 0 is skipped. So do I just have to find a number that skips all even numbers?


#2

@tylerz,

Javascript has the so-called modulo / remainder-operator if used it will return the rest-value.

9%3 You read it as nine modulo three is zero, as 9 divided by 3 has NO rest-value.
22%6 You read 22 modulo six is 4, as 22 divided by 6 will leave you with rest-value 4
16%8 You read it as sixteen modulo eight is zero, as sixteen divided by 8 has NO rest-value.
Now for the exercise 2/33:

var multiplesOfEight = [8,16,24,32,40,58];
                        |  |  |  |  |  |
      multiplesOfEight[0]  |  |  |  |  |
         multiplesOfEight[1]  |  |  |  |
            multiplesOfEight[2]  |  |  |
               multiplesOfEight[3]  |  |
                  multiplesOfEight[4]  |
                     multiplesOfEight[5]

multiplesOfEight[0] --> 8
multiplesOfEight[2] --> 24
multiplesOfEight[5] --> 58

multiplesOfEight[0]%8 --> 8%8 --> 0
multiplesOfEight[2]%8 --> 24%8 --> 0
multiplesOfEight[5]%8 --> 58%8 --> 2

google search
== the Book ==
javascript remainder operator site:developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators

== discussions / opinions ==
javascript remainder operator site:stackoverflow.com
= http://stackoverflow.com/questions/4467539/javascript-modulo-not-behaving
javascript array site:developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array


#3

@tylerz,

If you want to check if a number is even,
you can use

number%2 === 0

#4

@leonhard_wettengmx_n

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

Here is the example. but it wants it from 1 to 18 skipping odd numbers. It wants me to skip to the next iteration if the number is odd. And I am in ruby. what you posted earlier I am not that far in. Thank you though for the information. The typed part cleared up what the computer was reading.


#5

Thats how my code looks like as well. I run into an error as well.
Error: Oops, try again. It looks like your loop doesn't print out the numbers 18 to 0.

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

print i

end

What is the solution ?


#6

@ameyparab

Excellent question. All I changed was:

for i in 1..18
next if i % 3 == 0
print i
end

I changed it to 3 thinking that if 2 made if print odd numbers then maybe 3 will make it print even numbers.


#7

loool u guys are funny all u had to do was change == to != meaning not equal to

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


#8

@arraywhiz80918,

Please re-edit your Post

  • leave one blank-line above of your code
  • select your code in the Post
  • then =click= on the </>-symbol-of-this-editor

Your code will then be in a pre-code state
and you will be able to make/present the proper indentations.

or even better use
= http://discuss.codecademy.com/t/using-backticks-to-format-your-code/3697


#9

Thank you! I've just been skipping that part because I was so done, haha.


#10

Odd numbers are a multiple of two. We want to skip numbers which aren't a multiple of it. As every second integer is a multiple of two and therefore has the remainder equal to 0 we search for the one which have the remainder of 1.

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