Skip to content

Instantly share code, notes, and snippets.

@Ebonkuab
Last active January 9, 2016 00:52
Show Gist options
  • Save Ebonkuab/2ff4560aeeafc7c8df22 to your computer and use it in GitHub Desktop.
Save Ebonkuab/2ff4560aeeafc7c8df22 to your computer and use it in GitHub Desktop.
using ruby code to get the maximum value within a one dimensional array (LIGHTHOUSE WEB DEV BOOTCAMP)
# Find the maximum
def maximum(arr)
max = arr[0]
arr.each do |child|
if child > max
max = child
end
end
max
end
result = maximum([2, 42, 22, 02])
puts "max of 2, 42, 22, 02 is: #{result}"
# expect it to return nil when empty array is passed in
result = maximum([])
puts "max on empty set is: #{result.inspect}"
result = maximum([-23, 0, -3])
puts "max of -23, 0, -3 is: #{result}"
result = maximum([6])
puts "max of just 6 is: #{result}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment