Hi ;
This is my code . It is a shorter way with indexOf()
function filteredArray(arr, elem) {
let newArr = [];
// change code below this line
for( var i = 0 ; i<arr.length ; i++){
if (arr[i].indexOf(elem) === -1){
newArr.push(arr[i]);
}
}
// change code above this line
return newArr;
}
// change code here to test different cases:
console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));
function filteredArray(arr, elem) {
let newArr = [];
// change code below this line
let contador = 0;
let longitud = 0;
for (let i = 0; i<arr.length; i++){
for (var j=0; j<arr[i].length;j++){
longitud++;
if (arr[i][j]==elem){
contador++;
}
if (contador== 0 && longitud == arr[i].length ){
newArr.push(arr[i]);
}
}
longitud = 0;
contador= 0;
}
// change code above this line
return newArr;
}
// change code here to test different cases:
console.log(filteredArray([ ["trumpets", 2], ["flutes", 4], ["saxophones", 2] ], 2));
Hi,
They’re testing each subarray to see if it contains elem using .indexOf() array method.
.indexOf(elem) returns -1 if the array does not contain elem
If the test comes back negative (does not contain elem), then that subarray gets pushed onto newArr.
let arr = [10,50,75,"one"];
console.log(arr.indexOf(75); // logs 2, for index 2
console.log(arr.indexOf("cow"); // logs -1, there is no index for cow
function filteredArray(arr, elem) {
let newArr = [];
// loop through each subarray
for (var i = 0; i < arr.length; i++) {
// if subarray does not contain elem, then push subarray onto newArr
if (arr[i].indexOf(elem) === -1) { // -1 indicates elem not found in arr
newArr.push(arr[i]);
}
}
return newArr;
}