How to check if a string character is in a JavaScript array? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to check if a string character is in a JavaScript array? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m trying to make a JavaScript function that tells how many times a vowel was repeated in a given string.

Here’s what I have tried:

    function checkVowel(str) {
    vowels = ['a', 'e', 'i', 'o', 'u']
    str = "hello world"
    
    for(let i = 0; i < str.length; i++){
        if(str[i].includes(vowels)){
            console.log("worked")
        } else {
            console.log("not worked")
        }
    }
}
checkVowel()

How can I make this function check for each vowel rather than the entire array at once?

Answer

Is it this what you’re looking for?

function checkVowel(str) {
  const counts = Object.seal({ a: 0, e: 0, i: 0, o: 0, u: 0 });
  
  for (let char of str) {
    counts[char.toLowerCase()]++;
  }
  
  return counts;
}

console.log(checkVowel("hello world"));
We are here to answer your question about How to check if a string character is in a JavaScript array? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji