This is impossible


#1

I tried for 2 days straight to create a by_three? method that takes a single integer parameter, number, and returns true if that number is evenly divisible by three and false if not. IT NEVER FREAKING WORKS

Dis is my code

def by_three?(number)
multiples = (1..1000)
multiples.each { |num1, num2, num3| return num3 }
if (number) = num3
return true
else (number) != num3
return false
end
end

by_three?(9)


#2

Can you post your code so that we can try to help you please?


#3

Is this good?
Well obviously it's not good because it doesn't work but can you tell me what's wrong with it?

def by_three?(number)
multiples = (1..1000)
multiples.each { |num1, num2, num3| return num3 }
if (number) = num3
return true
else (number) != num3
return false
end
end

by_three?(9)


#4

Wow, there are several things you don't need here, and it's likely that you don't understand what you're asking to Ruby.

First, I would avoid using "?" for your method names, it can confuse you when you're going to use the ? operator on methods afterwards.

You don't need an array of mutliples to know if an integer is divisible by 3, you just need an if/else checking if the number modulo 3 equals 0 or not. If it does, it's divisible by 3. The operator for modulo is %.

Also, you don't need anything in front of the else, since else means "anything else" in the logical flow.

Finally, the = operator is not the same thing as the == operator. When you're checking a condtion like you're doing in your if statement, you need the == operator. The = operator is used to assign a value to something.


#5

thanks for the advice
:smile:


#6

hmm this is strange...
I changed my code to this:

def by_three(number)
if number % 3 == 0
return true
else
return false
end
end

by_three?(21)

but the monitor sends an error message like this:

undefined local variable or method `integer' for #

I don't know why this is happening, there is no 'integer' method or variable in any of my coding.
:confused:


#7

Your code works in the lab. Try refreshing the page or deleting your browser's cache.

And try your code here:

http://labs.codecademy.com/


#8

Thanks I'll try doing that (20 character minimum I couldn't just say ok)
:smile:


#9

Thank you arjofocolovi
IT WORKED
my eternal frustration has finally come to pass
:joy:


#10

That's great, good luck for the rest of the course.


#11

i did mine like this and it works like a charm:
def by_three?(number)
by_three=false
if number%3 ==0
by_three=true
return by_three
end
end


#12

I think you mean that your eternal frustration has passed. Having your eternal frustration come to pass means that you just got your eternal frustration