Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript or equals

Tags:

equals

Below some javascript code that should check wether it's one of those letters or not. However when I type "Hallo" for example it also counts the 'H', 'L', 'L

woord.charAt(i) == 'a' || 'e' || 'i' || 'o' || 'u' 

What did I do wrong ?

like image 265
user1825015 Avatar asked Nov 17 '25 14:11

user1825015


2 Answers

(woord.charAt(i) == 'a') || 'e' || 'i' || 'o' || 'u'
// evaluates to true or 'e'

It looks like you are trying to write code like you'd write a sentence, which doesn't translate well in this case. The above code shows what really happens with that expression.

You need to compare the character against each string individually or write a regular expression. The regex is more compact and would look like:

/[aeiou]/.test(word.charAt(i))
like image 111
TheZ Avatar answered Nov 19 '25 08:11

TheZ


Try using a different approach to the problem

    var c = woord.charAt(i);
    if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'){
    vowels++;
    }