Here is a function to count in an if
statement the vowels contained in a string:
def count_vowels(string)
sum = 0
n = 0
while n < string.length
if string[n] == "a"||string[n]=="i"||string[n]=="u"||string[n]=="e"||string[n]=="o"
sum += 1
end
n+=1
end
return sum
end
I found the repetitive string[n] ==
being redundant and replaced it with:
if string[n] == ("a"||"i"||"u"||"e"||"o")
However, in this code, the function does not return the correct counts. Why does the simplified if
statement not work here?