EDIT: removed explanation on why first works and second not since @camperextraordinaire did it already.
let’s avoid redundancy.
---- Now onto the challenge:
Let’s see if with some pseudo-code we can work this out - i’ll stick with your original idea instead of the alternative method we suggested, hoping to clarify some concept.
- loop into array
- expect each element to be an array with other element in it
- loop into nested array
- if nested array number is equal to 2nd argument
we don't want this array to be in the return array
Let’s convert this:
function filteredArray(arr, elem) {
let newArr = []; // we want to add into this.
// for each entry into the array
for(var i = 0; i<arr.length; i++){
// now we look at each sub array
var subArr = arr[i]; // reame so it's nicer to read
// let's say that we don't know if elem is inside
var isInside = false;
for(var j = 0; j< subArr.length; j++){
if(subArr[j] === elem) {
console.log("elem is in ", subArr)
isInside = true
}
}
console.log(subArr, ' contains ', elem, ' ? ', isInside)
}
return newArr;
}
filteredArray([[10, 8, 3], [14, 6, 23], [3, 18, 6]], 18);
/*
[ 10, 8, 3 ] ' contains ' 18 ' ? ' false
[ 14, 6, 23 ] ' contains ' 18 ' ? ' false
elem is in [ 3, 18, 6 ]
[ 3, 18, 6 ] ' contains ' 18 ' ? ' true
*/
There are tons of other approaches, this is, from my understanding what you had in mind.
Now you have to figure it out what you want to do once you know if isInside.
Hope this gives you a good start.
p.s. remember: don’t change the data while looping 