Hi,
the result isn’t repeated twice. You need to check if a given ‘sub’-array contains elem and only append those who don’t. Right now you append each ‘sub’-array for each element of it that is not elem.
Hope that makes sense.
What may I do to get an empty array. I am investigating methods, but I have not found it, which take elements from an array or eliminate it if it does not match the conditions.
function filteredArray(arr, elem) {
let newArr = [];
// Only change code below this line
for (var i=0;i<arr.length;i++){
for(var j=0;j<arr[i].length;j++){
if(arr[i][j]!=elem){
console.log(arr[i][j]);
}
}
}
// Only change code above this line
return newArr;
}
console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]],3));
For example you could check each sub-array if it contains the given elem by using indexOf() and only adding it, if it doesn’t.
But I guess there are alot of ways to solve this.
your referring to this? for loop challenge
the challenge doesnt ask you to use filter to pass you can use a standard for loop to loop through the arrays and for each sub array you can use indexOf. You only need to go one level deep if you use array.indexOf because indexOf will itself loop for you so that will be in place of using another inner loop.
if you want to use filter you can do the following in pseudo:
set some variable pointing to the arr arguement passed into your filteredArray function on this arr call the filter method (this will be your outer iteration of the loop meaning all the sub arrays and the filter will automatically do this), to this filter method pass it a callback using the same logic you would otherwise use… (hint: the indexOf method will take care of your the second loop you need, which would be each element inside each sub array)
outside of this filtering and sub-loop(meaning after you finish those operations) return your variable
these are just some ways you could solve it… im not sure if the challenge specifically demands that you use a for loop to solve it but you can try…
basically with the filter it will run the callback function once for each element, and constructs a new array of all the values for which the callback returns a value that is true
function filteredArray(arr, elem) {
// let newArr = [];
// Only change code below this line
// for (let i = 0; i < arr.length; i++) {
// if (arr[i].indexOf(elem) <0) {
// newArr.push(arr[i]);
// }
// }
// Only change code above this line
// return newArr;
//filter method:
const filtered= arr.filter(function(x){
return x.indexOf(elem)<0
})
return filtered;
}
console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 9]], 3));
Now I can obtain an accomplished challenge, but the rest of them I cannot get them
function filteredArray(arr, elem) {
let newArr = [];
// Only change code below this line
for(var i=0 ;i<arr.length;i++) {
let a =arr.indexOf(elem)
if (a !=-1 ) {
newArr.push(a);
}
}
// Only change code above this line
return newArr;
}
console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));