Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR-2: function method added #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions function.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe include some comments

def function(n)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe consider a more descriptive name for this function?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider naming the function a descriptive name to indicate what the function does

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def function(n)
def factorial(n)

Maybe this method could use a more descriptive name

if n == 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if n less than 0?

1
else
n * function(n-1)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe consider explicit returns for clarity?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider the case where n < 1, which will cause a stack overflow

end
Comment on lines +3 to +7
Copy link

@lina5147 lina5147 Mar 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider: Ternary operator? It would make the code more readable.

end
Comment on lines +2 to +8
Copy link

@geneminde geneminde Mar 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider if n < 0 or n is not an integer

Worth writing a simple loop for space efficiency?

Consider adding unit tests

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def function(n)
if n == 0
1
else
n * function(n-1)
end
end
def function(n)
if n == 0
1
else
n * function(n-1)
end
end

Can you make it more clear what this function is attempting to do?