Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. // First Attempt
  2.  
  3. /*
  4. let numberOfVowels = function(data){
  5. vowels = 0;
  6. for(let i=0; i<data.length; i++){
  7. if (data[i] == "a" || data[i] == "e" || data[i] == "i" ||data[i] == "o" ||data[i] == "u" ) {
  8. vowels += 1;
  9. }
  10. } return vowels;
  11. };
  12. */
  13.  
  14. // Second Attempt
  15.  
  16. /*
  17.  
  18. .indexOf() method returns the first index at which a given element can be can be found in the array, or -1 if it is present
  19.  
  20. */
  21.  
  22. let numberOfVowels = function(str){
  23. let vowel_list = "aeiouAEIOU";
  24. let vowels = 0;
  25. for (let i = 0; i < str.length; i++) {
  26. if (vowel_list.indexOf(str[i]) !== -1) {
  27. vowels += 1;
  28. }
  29. } return vowels;
  30. }
  31.  
  32. // Third Attempt
  33. /*
  34. .match() searches a string for a match against a regular expression, and returns the matches
  35.  
  36. regular expressions is a sequence of characters that forms a search pattern.
  37.  
  38. syntax is generally
  39.  
  40. /pattern/modifiers;
  41.  
  42. */
  43. function numberOfVowels(str) {
  44. return (str.match(/[aeiouAEIOU]/ig)||[]).length;
  45. }
  46.  
  47. console.log(numberOfVowels("orange"));
  48. console.log(numberOfVowels("lighthouse labs"));
  49. console.log(numberOfVowels("aeiou"));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement