I'm trying to filter out an array against another array, using Every() because Every() loop through an array and stop once it return false. However, I'm trying to make it return the new array from other array and stop once it's false.
For example, I have this word "chance" and this array vowels = ["a", "e", "i", "o", "u"]. So I wanted to match every letter in "chance" to this vowels array and extract "ch" and stop if "a" in "chance" match the vowels array.
Here's my code
function extract(str) {
var vowels = ["a", "e", "i", "o", "u"];
var word = str.split("");
var newletters = [];
var firstletter = str[0];
for (var i = 0; i <= vowels.length; i++) {
if (firstletter !== vowels[i]) {
word.every(function(letter){
if (letter.indexOf(vowels[i]) === -1 ){
return newletters.push(letter);
} else {
return false;
}
})
}
}
//return str;
}
console.log(extract("chance"));
I couldn't figure out how to make it works to get "ch" in new array.
var result = str.match(/^[^aeiou]*/) [0];.