Search code examples
javascriptarraysfilterpalindromesplice

Cant remove vowels from an array using filter that has multiple conditional statements


wordArr = ['a', 'g', 'e', 'f'];
wordArr = wordArr.filter(function(l){
            return l !== 'a' || l!== 'e'|| l!== 'i'|| l!== 'o' || l!== 'u'; 
       });

My result: my wordArr doesnt change

Desire output: ['g', f'];

it only works with one condition, if i only check for one vowel. How do i make this work for multiple conditions?

thank you in advanced.


Solution

  • Your conditions are incorrect. Consider just the following:

    return l !== 'a' || l!== 'e'
    

    If the string (inside l) is foo, the result is true since it's not 'a'.

    If the string is 'a', the result is true since it's not 'e':

    return l !== 'a' || l!== 'e'
    return false || true
    return true
    

    If the string is 'e', the result is true since it's not 'a':

    return l !== 'a' || l!== 'e'
    return true || false
    return true
    

    You need &&s instead.

    return l !== 'a' && l!== 'e'&& l!== 'i'&& l!== 'o' && l!== 'u'; 
    

    Or, even better:

    const wordArr = ['a', 'g', 'e', 'f'];
    console.log(wordArr.filter(str => !/[aeiou]/.test(str)));