-
Notifications
You must be signed in to change notification settings - Fork 5
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 3: find_max method #3
base: master
Are you sure you want to change the base?
Conversation
looks so good Chris |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Beautiful code! I'm wondering if you have any tests for this method
def find_max(array, max_index) | ||
max = nil | ||
if max_index == 0 | ||
puts "Array is empty" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure, but maybe "return" be more appropriate than "puts"? Otherwise, the code will continue to run after the print statement and then try to return nil
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's great idea, thank you for pointing it out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let me know if you have any questions on my comments
if max < array[index] | ||
max = array[index] | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if max < array[index] | |
max = array[index] | |
end | |
max = array[index] if max < array[index] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is another shorter option to consider!
index += 1 | ||
end | ||
end | ||
return max |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice job! I think this would correctly return the max. Have you considered writing tests for this method?
def find_max(array, max_index) | ||
max = nil | ||
if max_index == 0 | ||
puts "Array is empty" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a return statement would be better than a puts, then the code wouldn't continue to run and would just return immediately.
ie: return nil
def find_max(array, max_index) | ||
max = nil | ||
if max_index == 0 | ||
puts "Array is empty" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to add a return instead of a puts statement for this part? That way the code would stop running in case the array is empty.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function in general works great, I've added couple of comments please consider them.
|
||
# Find the maximum element between index 0 and max_index, exclusive | ||
|
||
def find_max(array, max_index) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be more readable to rename variable max_index into length?
def find_max(array, max_index) | |
def find_max(array, length) |
if max_index == 0 | ||
puts "Array is empty" | ||
else | ||
max = array[0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Max variable is initially assigned to nil and then to the array[0], is there a purpose to do that or initial value can be set to array[0]?
@@ -0,0 +1,19 @@ | |||
|
|||
# Find the maximum element between index 0 and max_index, exclusive |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is helpful, but you might consider (even if this is a small change) getting into the practice of adding a comment to your PR discussing what you changed/added, how it improves/impacts your code base -- for reviewer folx to be able to better understand your code's goals and scope quickly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Want to check the return value
index += 1 | ||
end | ||
end | ||
return max |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if max_index == 0, it prints "Array is empty" and returns max (ie nil), is that what we want?
No description provided.