Intermediate Algorithm Scripting - Drop it

Tell us what’s happening:
Describe your issue in detail here.

``````function dropElements(arr, func) {
let result=[];
for(let i=0;i<arr.length;i++){
if(func(arr[i])){
result.push(arr[i]);
}
}
console.log(result)
return result;
}

dropElements([1, 2, 3], function(n) {return n < 3; });
``````

the test result is : // running tests

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

should return

``````[1, 0, 1]
``````

why not [1,1]???
.

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

should return

``````[3, 9, 2]
``````

why not [3,9]???

. // tests completed // console output

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

Challenge: Intermediate Algorithm Scripting - Drop it

The instructions:

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.

Then return the rest of the array once the condition is satisfied, otherwise, `arr` should be returned as an empty array.

Note the bolded words: “…until the function `func` returns `true` … Then return the rest of the array once the condition is satisfied…”

Part of being a developer is reading things very closely.

thank you, i know my problem,i got it wrong.so i change it.now it’s ok!

``````function dropElements(arr, func) {
let result=[];
for(let i=0;i<arr.length;i++){
if(func(arr[i])){
result=arr.splice(i);
}
}
return result;
}
``````

Cool, good job.

I wrapped your answer in `[spoiler][/spoiler]` tags since it is a working solution to a curriculum challenge.

1 Like

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