Reputation: 1
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:
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
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