Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Write a function `removeEWords(sentence)` that accepts a sentence string as an
arg. The function should return a new string, containing only the words that
don't have the letter "e" in them.

Solve this using Array's `filter()` method.

Examples:

console.log(removeEWords('What time is it everyone?')); // 'What is it'
console.log(removeEWords('Enter the building')); // 'building'

What I have tried:

I've tried numerous things and am just banging my head against the wall.

What I am thinking - If word includes e, slice everything before/after from array then return new array. But if it has more than two words with e that'd destroy everything. I don't know. I've spent WAY too much time on this simple problem and only wish the lesson ahd given better examples/everything I find online doesn't use regex.


let removeEWords = function(sentence) {
    // Your code here
    words = sentence.split(" ");
    newString = '';
    newArray = words.filter(function(word, i, words) {

      if (word.includes('e')) {

      }
      }
    )
   
  };
Posted
Updated 3-Oct-22 17:42pm

1 solution

Figured it out. Some reason I thought I tied placing the NOT operator before the includes statement but must've only tried to put it before ---> !includes = FAIL.

let removeEWords = function(sentence) {
    // Your code here
    words = sentence.split(" ");
    anArray = [];
    newArray = words.filter(function(word, i, words) {

      if (!word.includes('e')) {
        anArray.push(word)
      }
      }
    )
    return anArray.join(" ");
  };
 
Share this answer
 
v2
Comments
Richard Deeming 4-Oct-22 5:21am    
filter[^] returns a new array containing the elements of the original array for which the callback returns true. There is no need to use a third array.
const words = sentence.split(" ");
const newArray = words.filter(w => !w.includes("e"));
return newArray.join(" ");
Chris Copeland 4-Oct-22 5:25am    
I didn't refresh my screen, you got in before me! 😆
Chris Aug2022 6-Oct-22 21:24pm    
So instead of voting it down like a douche add your own solution for me to accept. Oh, right, you're an elitist pig.
Richard Deeming 11-Oct-22 3:29am    
Insulting the people who are trying to help you is a great way to ensure nobody tries to help you in future. Keep it up, and you'lĺ end up getting kicked off the site.
Chris Copeland 4-Oct-22 5:24am    
Just to comment on this, that's not really what the filter() method is for. That method is supposed to return true or false to indicate whether the resulting array should contain that element. In this example you would only need to do:
anArray = words.filter(word => !word.includes('e'));

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900