adeel-justice
adeel-justice

Reputation: 1

Removing value from array in JS

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;
}

Upvotes: 0

Views: 46

Answers (1)

mgm793
mgm793

Reputation: 2066

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

function removeWordsWithChar(elems, string){
  return elems.filter(elem => !elem.toLowerCase().includes(string.toLowerCase()));
}

Upvotes: 1

Related Questions