Tell us what’s happening:
Hi, I don’t see what is wrong with my code
Your code so far
function filteredArray(arr, elem) {
let newArr = [];
// change code below this line
for (let i = 0; i < arr.length; i++) {
// find arrays
if(typeof arr[i] === "object") {
// check if it has elem and remove it
let holderArray = arr[i];
for (let j = 0; j < holderArray.length; j++) {
if (holderArray[j] === elem) {
// remove holderArray. from arr
arr.splice(i,1);
}
}
}else if(typeof arr[i] !== "object" && arr[i] === elem) {
arr.splice(i,1);
}
}
newArr.push(arr);
// 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));
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/iterate-through-all-an-arrays-items-using-for-loops/
Muedgar:
arr.splice(i,1);
Looks like you are splicing subarray instead of corresponding value in your subarray.
okay, so I must delete the elem?
Modify the function, using a for loop, to return a filtered version of the passed array such that any array nested within arr containing elem has been removed.
The code is passing every test except: filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3) should return [ ]
Can you post your new code?
function filteredArray(arr, elem) {
let newArr = [];
// change code below this line
for (let i = 0; i < arr.length; i++) {
// find arrays
if(typeof arr[i] === “object”) {
// check if it has elem and remove it
let holderArray = arr[i];
for (let j = 0; j < holderArray.length; j++) {
if (holderArray[j] === elem) {
// remove holderArray. from arr
arr.splice(i,1);
}
}
}else if((typeof(arr[i]) !== “object”) && (arr[i] === elem)) {
arr.splice(i,1);
}
}
newArr = arr;
// 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));
Can you format your code?
I can change my code, if you can help me fix it
I can’t help you because your code is not formatted and it’s very difficult to read.
select your code and hit preformat icon in your editor.
function filteredArray(arr, elem) {
let newArr = [];
// change code below this line
for (let i = 0; i < arr.length; i++) {
// find arrays
if(typeof arr[i] === “object”) {
// check if it has elem and remove it
let holderArray = arr[i];
for (let j = 0; j < holderArray.length; j++) {
if (holderArray[j] === elem) {
// remove holderArray. from arr
arr.splice(i,1);
}
}
}else if((typeof(arr[i]) !== “object”) && (arr[i] === elem)) {
arr.splice(i,1);
}
}
newArr = arr;
// 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));`
`
I am sorry, I am trying to format my code
function filteredArray(arr, elem) {
let newArr = [];
// change code below this line
for (let i = 0; i < arr.length; i++) {
// find arrays
if(typeof arr[i] === "object") {
// check if it has elem and remove it
let holderArray = arr[i];
for (let j = 0; j < holderArray.length; j++) {
if (holderArray[j] === elem) {
// remove holderArray. from arr
arr.splice(i,1);
}
}
}else if((typeof(arr[i]) !== "object") && (arr[i] === elem)) {
arr.splice(i,1);
}
}
newArr = arr;
// 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));
I see now.
After you are splice a subarray, you are skipping the next subarray. (Your index (i) goes up by one in the forloop but a subarray is removed)
Just include i--
in your code after your splice.
1 Like
I don’t understand why the code is returning the 2nd array
shimphillip:
Thanks alot; That is genius