Hi. I need to count vowels (a, e, i, o, u, not y) in JS and I wrote this code:
function getCount(str) {
let countVowels = /[aeiou]/g
let result = str.match(countVowels)
let final = result.length
if (final > 0) {
return result.length;
}
else {
return "0"
}
}
I have also tried: return 0 (without quotation marks) and nothing. I keep getting “null”, but I need to get a 0 if there are no vowels in the string I want to count. I’m trying to console log out “getCount(“my pyx”)” Please, help
I’ve edited your code 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.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
This will return null if there are no matches. If there are matches, it will return an array of the matches. You could check if result is null. If it is, then you would assign an empty array to result and then just return the length of result.
when you search for documentation, use keywords in google like “js match regex” and then pick the documentation that is appropriate and check the return values described.