Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function numVowels (str) {
- let count = 0;
- // Define a vowels array
- let vowels = ['a', 'e', 'i', 'o', 'u'];
- // Iterate over each character in the string
- for (let i=0; i<str.length; i++) {
- // check whether this character (in lower case) is a, e, i, o, or u (converting in lower case would reduce the amount of conditons by half since there would be no need to check A, E, I, O or U)
- if (vowels.indexOf(str[i].toLowerCase()) >= 0) {
- count++;
- }
- }
- // Print and return the number of vowels
- console.log(`The number of vowels in ${str} is = ${count}`);
- return count;
- }
Add Comment
Please, Sign In to add comment