Tell us what’s happening:
Describe your issue in detail here.
i kinda have an idea what needs to be done with this algorithm from the hints but this if structure seems to pass true all the time with or without the “!”
newArray=arr.shift()
} ```
**Your code so far**
```js
function dropElements(arr, func) {
console.log(arr)
let newArray=[];
for( let i = 0; i < arr.length; i++){
if (!func(arr[i])){
newArray=arr.shift();
console.log(newArray)
}
}
return newArray;
}
dropElements([1, 2, 3], function(n) {return n < 3; });
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36
Why are you doing this? You’re mutating the original array arr and then setting newArr equal to the mutated array, which is redundant. You should also consider that you’re looping over an array which is being mutated.
Using the shift method is fine, and mutating the original array is fine. I’m guessing you were trying to keep track of something with newArr, but what you end up doing is just resetting newArr to equal the mutated array arr. It’s not going to keep you from passing; it’s just excessive.
The main thing you need to worry about is that when you mutate arr using the shift method you change the value at the index you’re evaluating arr[i] at in your for loop.
Yes, my mistake on that point. You are correct about the return value. I was focused on just returning the mutated array as being more efficient and didn’t pay attention to the actual return value of arr.shift().
Apologies to @fredsmith27182 for distracting you from the main point that you’re looping over an array which is being mutated.
Gotcha, I wasn’t trying to be too critical, I just didn’t want @fredsmith27182 to get the wrong idea And I see your point, the return value on the shift doesn’t really come into play here anyway.
oh not a problem, im not sure if the for loop is the right idea because i feel the test is not working, . when i remove the ! , it still output the same logic
You can use a for loop here if you want but you’ll need to take a little more care in how you do the counter variable because when you do a shift on the array you are decreasing its length by one, so you can’t use arr.length as the test because the length is changing each iteration through the loop. Do you see how that is causing trouble?
While I’m typing here, I might suggest that while a for loop can be used, there might be an alternative loop in which you could use arr.length in the test condition.
the while loop is much better for this application . less things to mess up on. i actually found another mistake. i shouldnt use arr[i] if im mutating an array. because i gets incremented.