Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // First Attempt
- /*
- let numberOfVowels = function(data){
- vowels = 0;
- for(let i=0; i<data.length; i++){
- if (data[i] == "a" || data[i] == "e" || data[i] == "i" ||data[i] == "o" ||data[i] == "u" ) {
- vowels += 1;
- }
- } return vowels;
- };
- */
- // Second Attempt
- /*
- .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
- */
- let numberOfVowels = function(str){
- let vowel_list = "aeiouAEIOU";
- let vowels = 0;
- for (let i = 0; i < str.length; i++) {
- if (vowel_list.indexOf(str[i]) !== -1) {
- vowels += 1;
- }
- } return vowels;
- }
- // Third Attempt
- /*
- .match() searches a string for a match against a regular expression, and returns the matches
- regular expressions is a sequence of characters that forms a search pattern.
- syntax is generally
- /pattern/modifiers;
- */
- function numberOfVowels(str) {
- return (str.match(/[aeiouAEIOU]/ig)||[]).length;
- }
- console.log(numberOfVowels("orange"));
- console.log(numberOfVowels("lighthouse labs"));
- console.log(numberOfVowels("aeiou"));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement