I'm having trouble with a For loop array. I need to retrieve the data that is opposite of a given function, but when I use arr[i] != elem
, it prints out the entire array. On the other hand, if I use arr[i] == elem
, it gives me the array that I don't want. I still can't figure out why it's not working with != (not equal)
.
function getOppositeArray(arr, elem) {
let newArr = [];
// change code below this line
for(let i = arr.length -1; i >= 0 ; i--) {
for(let j = arr[i].length-1;j >= 0;j--) {
if(arr[i][j] !== elem) {
newArr.push(arr[i]);
}
}
}
// change code above this line
return newArr;
}
console.log(getOppositeArray([ ["trumpets", 2], ["flutes", 4], ["saxophones", 2] ], 2));
The desired result is ["flutes", 4]
. I apologize if this question has been asked before. I have searched for an answer on Google but couldn't find one.
Thank you in advance for your assistance!