Search code examples
javascriptarray-filter

Write a function that should return a new string, containing only the words that don't have the letter "e" in them in javascript


what am I doing wrong in the code that I mentioned below?

My outputs are [ 'What', 'time', 'is', 'it', 'everyone?' ] and [ 'building' ]. These outputs should be string because I used join method. Also the first output is totally wrong.

let removeEWords = function(sentence) {
  debugger
  let arrayedSentence = sentence.split(" ");
  let newArr = [];
  return arrayedSentence.filter(function(el) {
    if (!el.includes("e")) {
      newArr.push(el);
    }
    return newArr.join(" ");
  })

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


Solution

  • You need to 'filter' and not add to an array - you are using filter as a forEach

    const removeEWords = sentence => sentence
      .split(" ")
      .filter(word => !word.includes("e")) // this returns true to return or false to drop
      .join(" ");
    console.log(removeEWords('What time is it everyone?')); // 'What is it'
    console.log(removeEWords('Enter the building')); // 'building'