skip to Main Content

I have tried passing strings to the getCount function that contain ‘aeiou’ and not but the value of vowelsCount is always 0.
I tried using string includes() function using the documentation, but it always returns count as 0, I have the code with which we can get the exact number of vowels in a string, but I prefer to use the approach taught by MDN docs.
This is the approach i prefered:

const vowelCount = str => {
 
let voweslCount = 0;

for ( let char of str){

if (str.includes('aeiou'))

voweslCount++;

}

return voweslCount;

}

2

Answers


  1. Try splitting up the problem into smaller easier to reason about functions:

    /**
     * @const {Array<string>} VOWELS - An array of english vowel characters.
     * Note under ~100 or so elements, Array lookup is faster than a Set despite O(n) vs O(1) lookup time complexity, respectively.
     */
    const VOWELS = ['a', 'e', 'i', 'o', 'u'];
    
    /**
     * Checks if a character is a vowel.
     * @param {string} char - The character to check.
     * @returns {boolean} True if the character is a vowel, otherwise false.
     */
    function isVowel(char) {
      // Convert character to lowercase to handle both uppercase and lowercase letters.
      return VOWELS.includes(char.toLowerCase());
    }
    
    /**
     * Counts the number of vowels in a given string.
     * @param {string} str - The string to count vowels in.
     * @returns {number} The number of vowels in the string.
     */
    function vowelCount(str) {
      let vowelsCount = 0;
      for (let char of str) {
        if (isVowel(char)) {
          vowelsCount++;
        }
      }
      return vowelsCount;
      // Alternatively: return Array.from(str).filter(isVowel).length;
    }
    
    // Example usage.
    const exampleString = "Hello World!";
    console.log(`Number of vowels in "${exampleString}": ${vowelCount(exampleString)}`);
    Login or Signup to reply.
  2. You can also start by creating an array containing each letter of the string, then transform each letter of that array into an array with map()

    function countVowels(str){
      let counter = 0
      let arrayOfStr = Array.from(str.toLowerCase())
      let arrayOfArray = arrayOfStr.map(elem=>[elem])
      for(let i=0;i<arrayOfArray.length;i++) {
        if(arrayOfArray[i].includes('a') || arrayOfArray[i].includes('e') || arrayOfArray[i].includes('i') || arrayOfArray[i].includes('o') || arrayOfArray[i].includes('u') || arrayOfArray[i].includes('y')){
          counter+=1
        }
      }
      return (counter) 
    }
    
    
    
    console.log(countVowels('Hello World'))
    
    /* you can also loop through arrayOfArray with forEach as below*/
    
    function countVowels2(str){
      let counter = 0
      let arrayOfStr = Array.from(str.toLowerCase())
      let arrayOfArray = arrayOfStr.map(elem=>[elem])
      arrayOfArray.forEach((elem)=>{
        if(elem.includes('a') || elem.includes('e') || elem.includes('i') || elem.includes('o') || elem.includes('u') || elem.includes('y')){
          counter+=1
        }
      })
      return counter
    }
    
    console.log(countVowels2('Hello World'))

    function. Then check with includes() if each array containing letters includes vowels

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search