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:
- an array of strings
- 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;
}
1 Answer 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
lang-js
arr2.filter(x=>!x.toLowerCase().includes(strLength.toLowerCase()))if (thisWord.toLowerCase().indexOf(stringLength.toLowerCase()) === -1)