1. Why Methods?


#1


https://www.codecademy.com/courses/ruby-beginner-en-ET4bU/0/1?curriculum_id=5059f8619189a5000201fbcb

Hello,

I did not understand what "i" is doing here. Is it executing for each "i"?

e.g. n=9 so is for each i between 2 and (9-1)=8 "n%i==0" executing or not? I mean for n=9, i= 2,3,4,5,6,7,8 respectively. For my understanding of modulo n%i==0 is_prime should be true. I would be very glad if you can help me.


def prime(n)
  puts "That's not an integer." unless n.is_a? Integer
  is_prime = true
  for i in 2..n-1
    if n % i == 0
      is_prime = false
    end
  end
...


#2

i is the individual sample taken from the range 2..n-1.


#3

Thank you! I understood finally :slight_smile:


#4

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