3

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.

7
  • 1
    every returns a boolean - you can't change how a method works - how about using filter or map instead? Commented Jul 16, 2017 at 4:58
  • Yes, I did consider using filter with every but filter loops through all objects in an array. I wanted it to stop looping once it returns false... Commented Jul 16, 2017 at 5:04
  • 1
    Why not using a regex? var result = str.match(/^[^aeiou]*/) [0];. Commented Jul 16, 2017 at 5:06
  • 1
    Why not a simple for loop ? Commented Jul 16, 2017 at 5:08
  • Y is a vowel too btw :) Commented Jul 16, 2017 at 5:33

2 Answers 2

1

You can join vowels and use the resulting string in a regex:

function extract(str) {
  var vowels = ["a", "e", "i", "o", "u"];
  var regex = new RegExp("(.*?)[" + vowels.join("") + "]");
  if (str.match(regex) != null && str != "") {
    console.log(str.match(regex)[1]);
    //newletters.push(str.match(regex)[1]);
  }
}

extract("chance");

Sign up to request clarification or add additional context in comments.

2 Comments

What about: "" and "tv"?
Both extract(""); (empty string) and extract("rythm"); (no vowels) throw errors.
0

You cannot do it.
every() function is designed to return a boolean for each tested element.
But in it, you could add letters in newletters array and then return true.
And in case of vowel matching, just return false to end the every() function. In this way, at the end of the processing, newletters is valued as required.

 for (var i = 0; i <= vowels.length; i++) {
    if (firstletter !== vowels[i]) {
      word.every(function(letter){
        if (letter.indexOf(vowels[i]) === -1 ){
            newletters.push(letter);
            return true;
         }             
         return false;            
      })

    }
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.