How do I return -1 using match() method if a search is not found?
//Write a JavaScript function that accepts a string as a parameter and counts the number of vowels within the string. function countVowels(string) { var vowels = [], regEx = /[aeiou]/gi, counter = 0; if(string === undefined) string = ''; vowels = string.match(regEx); counter = vowels.length; return vowels === null ? -1 : counter; } console.log('The quick brown fox') // logs 5 But I want the output to be -1 if a vowel is not found in the string. I have tried to use an if else statement like this: if(counter > 0) return counter; else return -1; // This doesn't work too. Outputs a blank screen I don't know if it is because of the match() method I used. I googled the return value of the match() method if no match is found and it is 'null'. That is why I used it in my code above: return vowel === null ? -1 : counter; //outputs a blank screen but it still doesn't work. What's the problem?