0

Im new to software development and have been taking a class, im having an issue with finding out how to remove items in an array based on a 2nd argument character

Write a function "removeWordsWithChar" that takes 2 arguments:

  1. an array of strings
  2. a string of length 1 (ie: a single character) It should return a new array that has all of the items in the first argument except those that contain a character in the second argument (case-insensitive).

Examples:

----removeWordsWithChar(['aaa', 'bbb', 'ccc'], 'b') --> ['aaa', 'ccc']

----removeWordsWithChar(['pizza', 'beer', 'cheese'], 'E') --> ['pizza']

function removeWordsWithChar(arrString, stringLength) {
  const arr2 = [];
 for (let i = 0; i < arrString.length; i++) {
   const thisWord = arrString[i];
    if (thisWord.indexOf(stringLength) === -1) {
     arr2.push(thisWord);
    }
  }
return arr2;
}
5

1 Answer 1

1

As they said in comments you can use filter like this:

function removeWordsWithChar(elems, string){
  return elems.filter(elem => !elem.toLowerCase().includes(string.toLowerCase()));
}
Sign up to request clarification or add additional context in comments.

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.