# Intermediate Algorithm Scripting - Drop it

### Tell us what’s happening:

Hello, I would like to know what’s the problem here .

``````function dropElements(arr, func) {
let newArr = [];
for(let i=0 ; i<arr.length; i++){
if(arr[i]!== arr[i])  {
let filtered = arr.filter(func);
return newArr = filtered;
}else {
let duplicatedElement = []
for(const num in arr){
for(const num2 in arr){
if(num === num2){
continue;
}else {
if(arr[num] === arr[num2]) {
duplicatedElement.push(arr[num])
}

}
}  [...new Set(duplicatedElement)]
}
let filtered = arr.filter(func);
return filtered.concat([...new Set(duplicatedElement)])
}
}
}

console.log(dropElements([0, 1, 0, 1], function(n) {return n ===1; }));
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36`

### Challenge Information:

Intermediate Algorithm Scripting - Drop it

Can you say more about what’s happening? What tests are failing? Where did you get stuck investigating?

1 Like

i failed this one : dropElements([0, 1, 0, 1], function(n) {return n ===1; })
its output is [1, 1, 0, 1] . It should return [1, 0, 1]

Ok, where did you get stuck investigating? You logic for finding the first element that returns a true from the callback function seems really complicated.

1 Like

basically as u can see in the code i’ve struggled with returning the duplicated elements so i’ve tried to solve this by searching for them by using if…else statement , everything seems to work fine but the test : dropElements([0, 1, 0, 1], function(n) {return n ===1; }).
Do you suggest anything to improve the code so it returns the desired output?

Can you write a function that gives the index of the first element that returns true from the callback function?

1 Like

Hello @jabbarzineb36! Welcome to the forum!

This is a tricky challenge, but consider what the callback actually does, and what the code needs to watch for.

Does this help?
Keep up the good progress!

Happy Coding!

if i understand your question correctly , is this what do you mean ?

``````let firstIndex = ""
var ismatch =false;
for(var a=0; a < arr.length ; a++){
for(var b=a+1; b< arr.length ; b++){
if(arr[a]=== arr[b]){
firstIndex = arr.indexOf(arr[a]);
ismatch = true;
break;
}

}
}console.log(firstIndex)
``````

//output : 1

Note

1. var is obsolete - don’t use it

``````let firstIndex = 0;
let ismatch = false;
for (let a = 0; a < arr.length; a++) {
for(let b = a + 1; b< arr.length; b++) {
if (arr[a] === arr[b]) {
firstIndex = arr.indexOf(arr[a]);
ismatch = true;
break;
}
}
}
console.log(firstIndex);
``````

A few questions

1. why do you have two loops

2. why are you checking if `arr[a] === arr[b]`

3. where is the callback function being used

first I used the two for loops to loop through the array two times basically to check if two elements of the array are the same , in other words to search for the duplicated elements .
if there is a match that’s when to return the index of the first element that is duplicated in the array .
last , can you please tell me what’s the matter of checking the index of the first duplicated element?

Why are you checking if two of the array items are the same though?

Given the array `arr`, iterate through and remove each element starting from the first element (the 0 index) until the function `func` returns `true` when the iterated element is passed through it.

to see if there are duplicated item that is repeated two times cause that was the problem why i am failing the test , for example: dropElements([1, 2, 3, 9, 2], function(n) {return n > 2;}),
// the output is [3, 9] , it should be [3, 9 , 2]

I think you may have misunderstood the requirements. I would suggest you re-read them again.

The most basic version of the requirements would be:

If the callback function returns true when it is passed a given element, return the rest of the array starting from that element.

Yeah, that’s not what the instructions are asking for. You should not have a double loop and you must use the callback function.

``````function dropElements(arr, func) {
return arr.filter(func);

}

console.log(dropElements([1, 2, 3, 9, 2], function(n) {return n  > 2; }));

``````

i edited my code so its following the instructions but still im so confused cause it’s failing dropElements([1, 2, 3, 9, 2], function(n) {return n > 2;}) and dropElements([0, 1, 0, 1], function(n) {return n === 1;}).

It’s not quite following the instructions. The instructions don’t say to filter out every element that fails the callback.

Once you find one passing element, you return the entire rest of the array.

yes i figured out the problem , thank you!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.