Created
May 8, 2016 03:00
-
-
Save shaselton/33304f0b41a96359e6ce8b5dfa4fbab8 to your computer and use it in GitHub Desktop.
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
# Description: | |
# Implement a function called makeAcronym that returns the first letters of each word in a passed in string. | |
# Make sure the letters returned are uppercase. | |
# If the value passed in is not a string return 'Not a string' | |
# If the value passed in is a string which contains only characters other than spaces and alphabet letters, return 'Not letters' | |
# EXAMPLES: | |
# 'Hello codewarrior' -> 'HC' | |
# '42' -> 'Not letters' | |
# 42 -> 'Not a string' | |
# [2,12] -> 'Not a string' | |
# {name: 'Abraham'} -> 'Not a string' | |
def makeAcronym(str) | |
return 'Not a string' unless str.is_a? String | |
return 'Not letters' if str.to_i.to_s == str | |
str.split(' ').map(&:upcase).inject(''){ |acronym, word| acronym + word[0]} | |
end | |
puts makeAcronym('Hello codewarrior') | |
puts makeAcronym('42') | |
puts makeAcronym(42) | |
puts makeAcronym([2,12]) | |
puts makeAcronym({name: 'Abraham'}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment