Search code examples
rubyoperator-keywordternary

Why is return skipping the value produced in 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?


Solution

  • It returns the argument because you told to do so. Try this.

    def FirstFactorial(num)
    
      return (num == 0 ? 1 : num * FirstFactorial(num - 1))
    
    end