Created
January 3, 2013 06:36
-
-
Save kmandreza/4441287 to your computer and use it in GitHub Desktop.
Write a method shortest_string which takes as its input an Array of Strings and returns the shortest String in the Array. For example: # 'cat' is 3 characters long
shortest_string(['cat', 'zzzzzzz', 'apples']) # => "cat" # '' is 0 characters long, but it's the only string
shortest_string(['']) # => '' shortest_string([]) # => nil
If the input Ar…
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
| # shortest_string is a method that takes an array of strings as its input | |
| # and returns the shortest string | |
| # | |
| # +array+ is an array of strings | |
| # shortest_string(array) should return the shortest string in +array+ | |
| # | |
| # If +array+ is empty the method should return nil | |
| def shortest_string(array) | |
| shortest = nil | |
| array.each do |x| | |
| if shortest.nil? || shortest.length > x.length | |
| shortest = x | |
| end | |
| end | |
| shortest | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment