Skip to content

Instantly share code, notes, and snippets.

@barbchoy
Last active September 29, 2017 05:45
Show Gist options
  • Save barbchoy/9e87800248b97c69cd42ee54a559a9aa to your computer and use it in GitHub Desktop.
Save barbchoy/9e87800248b97c69cd42ee54a559a9aa to your computer and use it in GitHub Desktop.
null created by barbchoy - https://repl.it/Br7N/2770
# Write a method that takes a string and returns the number of vowels
# in the string. You may assume that all the letters are lower cased.
# You can treat "y" as a consonant.
#
# Difficulty: easy.
def count_vowels(string)
i=0
num_vowels=0
vowels=["a","e","i","o","u"]
letters=string.split('')
while i<letters.length
j=0
while j<vowels.length
if letters[i]===vowels[j]
num_vowels=num_vowels+1
end
j+=1
end
i+=1
end
return num_vowels
end
# These are tests to check that your code is working. After writing
# your solution, they should all print true.
puts("\nTests for #count_vowels")
puts("===============================================")
puts('count_vowels("abcd") == 1: ' + (count_vowels('abcd') == 1).to_s)
puts('count_vowels("color") == 2: ' + (count_vowels('color') == 2).to_s)
puts('count_vowels("colour") == 3: ' + (count_vowels('colour') == 3).to_s)
puts('count_vowels("cecilia") == 4: ' + (count_vowels('cecilia') == 4).to_s)
puts("===============================================")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment