I'm making a method in Ruby to count how many vowels are in a string. I figured it out, but at first I made the mistake of not repeating "variable==" after every '||'. The correct way seems inefficient. Is there a quicker way to do this and get the same result?
See example:
CORRECT:
def count_vowels(string)
vnum = 0
n = 0
while n < string.length
if string[n] == "a"||string[n] == "e"||string[n] =="i"||string[n] =="o"||string[n] =="u"||string[n] =="y"
vnum += 1
end
n += 1
end
return vnum
end
INCORRECT:
def count_vowels(string)
vnum = 0
n = 0
while n < string.length
if string[n] == "a"|| "e"||"i"||"o"||"u"||"y"
vnum += 1
end
n += 1
end
return vnum
end