Skip to content

Instantly share code, notes, and snippets.

@sidazhang
Last active December 13, 2015 20:59
Show Gist options
  • Save sidazhang/4974389 to your computer and use it in GitHub Desktop.
Save sidazhang/4974389 to your computer and use it in GitHub Desktop.
Shortest String
# 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 = 0
array.length.times do |i|
if i == 0
shortest = array[i]
elsif
shortest.length > array[i].length
shortest = array[i]
else
shortest
end
end
if array.length == 0
shortest = nil
else
shortest
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment