Created
January 3, 2013 06:37
-
-
Save kmandreza/4441292 to your computer and use it in GitHub Desktop.
Write a method longest_string which takes as its input an Array of Strings and returns the longest String in the Array. For example: # 'zzzzzzz' is 7 characters long
longest_string(['cat', 'zzzzzzz', 'apples']) # => "zzzzzzz"
If the input Array is empty longest_string should return nil.
This file contains hidden or 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) | |
| longest = nil | |
| array.each do |x| | |
| if longest.nil? || longest.length < x.length | |
| longest = x | |
| end | |
| end | |
| longest | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment