I have this code:
def FirstFactorial(num)
num == 0 ? 1 : num * FirstFactorial(num - 1)
return num
end
however, the result keeps returning the original argument. How can I return the result created by my ternary operator?
I have this code:
def FirstFactorial(num)
num == 0 ? 1 : num * FirstFactorial(num - 1)
return num
end
however, the result keeps returning the original argument. How can I return the result created by my ternary operator?
You need to set num equal to the result of the ternary operator. Or just return it as in Elyasin's answer.
def FirstFactorial(num)
num = num == 0 ? 1 : num * FirstFactorial(num - 1)
return num
end
Edit: Although, remember that in ruby the result of the last line is returned, so you could just say:
def FirstFactorial(num)
num == 0 ? 1 : num * FirstFactorial(num - 1)
end
It returns the argument because you told to do so. Try this.