Skip to content

Instantly share code, notes, and snippets.

@rodloboz
Created June 27, 2018 10:37
Show Gist options
  • Save rodloboz/7af6d4f178ecffe97ae03c60169b8e03 to your computer and use it in GitHub Desktop.
Save rodloboz/7af6d4f178ecffe97ae03c60169b8e03 to your computer and use it in GitHub Desktop.
Acronymize
def acronymize(string)
# TODO: write a method that returns
# the acronym for the given string
# 1. Split the string into words
# 2. Take the first letter of each word
# & capitalize them
# 3. Join the first letters together
# 4. Return the result
words = string.split(" ")
# p words - debugging
acronym = ""
words.each do |word|
acronym += word[0].upcase
end
# ["freq", "asked", "questions"] - initial arr
# [ "F", "A", "Q"] -> transformed arr
words.map do |word|
word[0].upcase
end.join
# acronym
end
# TDD - Tests
puts acronymize("Central Intelligence Agency") == "CIA"
puts acronymize("central intelligence agency") == "CIA"
puts acronymize("Central Intelligence Agency".upcase) == "CIA"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment