Created
December 15, 2013 23:12
-
-
Save dstavis/7979684 to your computer and use it in GitHub Desktop.
Exercise 19: Finding the longest string in an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# longest_string is a method that takes an array of strings as its input | |
# and returns the longest string | |
# | |
# +array+ is an array of strings | |
# longest_string(array) should return the longest string in +array+ | |
# | |
# If +array+ is empty the method should return nil | |
def longest_string(array) | |
return nil if array.empty? | |
longest_length = 0 | |
array.each |string| | |
longest_length = string.length if string.length > longest_length | |
end#each | |
array.each |string| | |
if string.length == longest_length | |
longest_string = string | |
end#if | |
end#each | |
return longest_string | |
end#def |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment