Vowel Counter in JavaScript

hey i started learning JavaScript Last week i wnt to code a Vowel Counter i dont know where is the mistake

///////


function getCount(str) {
  var vowelsCount = 0;
  const arr = [ "a","e","i","o","u" ] ;
   
for (let i = 0 ; i < str.length ; i++ ) { 

if ( str[i] in arr ) { vowelsCount += 1 ;}

}   
  
  
  
  
  return vowelsCount;
}
///////

The in operator in js is quite different from python: it is used to check if something is actually a property of an object ( here is a link: in operator in js - Flavio Copes )

If you want to check if an element is included in an Array you can use the includes method; something like if ( arr.includes(str[i]) ) ^^

1 Like

Aha okey I understand :smiley:
Thank You Very Much !

1 Like

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

1 Like