# Help with Intermediate Algorithm Scripting: Drop it

Help with Intermediate Algorithm Scripting: Drop it
0

#1

I was checking out the solution to this challenge, to see if my intuition was right. It was, for the most part, except for one radical difference:

``````
…function dropElements(arr, func) {
// drop them elements.
var times = arr.length;
for (var i = 0; i < times; i++) {
if (func(arr[0])) {
break;
} else {
arr.shift();
}
}
return arr;
}

// test here
dropElements([1, 2, 3, 4], function(n) {return n >= 3;})…
``````

I wouldn´t have created the times variable at all, instead I would have just added “< arr.length” inside the for loop, as usual. I tried that, but it doesn´t work. I don´t really understand the difference between both of these. Could anyone explain?

#2

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (`</>`) will also add backticks around text.

Note: Backticks are not single quotes.

#3

So follow the logic: Loop through the array a certain number of times, and, if the first element each time doesn’t return true from a given function, remove it.

Each time, we’re shortening the array, so arr.length will be changing. But we still may want to process through every element, so we assign arr.length to a variable before, so that it remains constant (thus the famous coder quote, “Variables won’t, constants aren’t.”)

If we used arr.length each time, that number will change. But `i` is going to increment, regardless – by using arr.length as it gets shorter and shorter, we would eventually have elements not getting hit.

If, instead, we used a do/while, we could simply remove elements and, while the `arr.length && !func(arr[0])` values return true, keep removing the first element. That would have much the same end result, without the pesky `i` and the extra variable for `times`.

#4

NOW I get it. Thank you so much for such a clear explanation.

#5

Glad it helped, pay it forward!

#6

solution:
function dropElements(arr, func) {
// Drop them elements.
let narr=arr.filter((x)=>{
return func(x);
}

)
return narr;
}
cases not running:
dropElements([0, 1, 0, 1], function(n) {return n === 1;});
dropElements([1, 2, 3, 9, 2], function(n) {return n > 2;})

#7

Did you read the test spec? It is NOT that you filter out all instances that pass, it is that you stop at the FIRST instance of pass and return the remaining elements.

#8

It’s a challenge to give constructive hints without simply telling you what to do. There are a few ways this can be handled, but what it boils down to is this:

1. Does the first el return true from the passed func? If so, return the array as it stands.
2. If it doesn’t, remove that first element, and run step one again, as long as the array contains some elements.
3. If no elements return true, simply return an empty array.