Conversation
|
|
||
| def function(n) | ||
| if n == 0 | ||
| 1 |
| @@ -0,0 +1,9 @@ | |||
|
|
|||
| def function(n) | |||
There was a problem hiding this comment.
Let's make this function dryer. We could write a ternary statement instead of spreading the code out into multiple lines.
|
|
||
| def function(n) | ||
| if n == 0 | ||
| 1 |
There was a problem hiding this comment.
Let's write 'puts' in front of the code we want to print to the screen.
| @@ -0,0 +1,9 @@ | |||
|
|
|||
| def function(n) | |||
| if n == 0 | |||
There was a problem hiding this comment.
Did you mean to include "return" before the "1"?
| @@ -0,0 +1,9 @@ | |||
|
|
|||
| def function(n) | |||
There was a problem hiding this comment.
function doesn't seem like a meaningful name; it should probably be called 'factorial' or something
|
|
||
| def function(n) | ||
| if n == 0 | ||
| 1 |
There was a problem hiding this comment.
I think you need to explicitly return 1 here and, on line 6, return n * function(n-1)
| @@ -0,0 +1,9 @@ | |||
|
|
|||
| def function(n) | |||
| if n == 0 | |||
There was a problem hiding this comment.
if n is a float, this function might go on forever since it will never == zero. Perhaps <= would be better?
| 1 | ||
| else | ||
| n * function(n-1) | ||
| end |
There was a problem hiding this comment.
can shorten this to
| end | |
| n == 0 ? 1 : n * function(n-1) |
| @@ -0,0 +1,9 @@ | |||
|
|
|||
| def function(n) | |||
There was a problem hiding this comment.
Maybe rename this function to factorial?
| if n == 0 | ||
| 1 | ||
| else | ||
| n * function(n-1) |
There was a problem hiding this comment.
If you change the name of the function, you have to change it here too.
| @@ -0,0 +1,9 @@ | |||
|
|
|||
| def function(n) | |||
There was a problem hiding this comment.
function naming should be more clear as to what it does
|
|
||
| def function(n) | ||
| if n == 0 | ||
| 1 |
| @@ -0,0 +1,9 @@ | |||
|
|
|||
| def function(n) | |||
| if n == 0 | ||
| 1 | ||
| else | ||
| n * function(n-1) |
There was a problem hiding this comment.
| n * function(n-1) | |
| return n * function(n-1) |
|
|
||
| def function(n) | ||
| if n == 0 | ||
| 1 |
| if n == 0 | ||
| 1 | ||
| else | ||
| n * function(n-1) |
There was a problem hiding this comment.
Should handle the initial case of n < 0
|
|
||
| def function(n) | ||
| if n == 0 | ||
| 1 |
There was a problem hiding this comment.
could add "return" to make it clearer
|
|
||
| def function(n) | ||
| if n == 0 | ||
| 1 |
There was a problem hiding this comment.
What is the 1 supposed to be set to? Are you trying to return it? Please write it out
| @@ -0,0 +1,9 @@ | |||
|
|
|||
| def function(n) | |||
There was a problem hiding this comment.
This should have a more descriptive name. Function dos not let us know what to use it for, and may be a reserved word in ruby.
This method calculates a factorial