Guest User

Untitled

a guest
Dec 11th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. function numVowels (str) {
  2. let count = 0;
  3.  
  4. // Define a vowels array
  5. let vowels = ['a', 'e', 'i', 'o', 'u'];
  6.  
  7. // Iterate over each character in the string
  8. for (let i=0; i<str.length; i++) {
  9. // 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)
  10. if (vowels.indexOf(str[i].toLowerCase()) >= 0) {
  11. count++;
  12. }
  13. }
  14.  
  15. // Print and return the number of vowels
  16. console.log(`The number of vowels in ${str} is = ${count}`);
  17. return count;
  18. }
Add Comment
Please, Sign In to add comment