Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading