I use basic JavaScript to count the number of vowels in a string. Below is the code, but I would like it to clear a bit. Does .includes() help at all, considering it to be a string? I would like to use something like string.includes("a", "e", "i", "o", "u") , if at all it is possible to clear the conditional statement. Also, do I need to convert the input to a string?
function getVowels(str) { var vowelsCount = 0; //turn the input into a string var string = str.toString(); //loop through the string for (var i = 0; i <= string.length - 1; i++) { //if a vowel, add to vowel count if (string.charAt(i) == "a" || string.charAt(i) == "e" || string.charAt(i) == "i" || string.charAt(i) == "o" || string.charAt(i) == "u") { vowelsCount += 1; } } return vowelsCount; }
source share