Drop element challenge

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

Challenge: Drop it

Link to the challenge:

let newArray=[]
newArray=arr.shift()

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.

the hint recommended the shift method. but ill look more into it.

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.

Let’s look at the MDN docs for shift:

Return value

The removed element from the array; undefined if the array is empty.

So newArray is being set to the removed element from the array, not the actual array.

1 Like

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.

2 Likes

Gotcha, I wasn’t trying to be too critical, I just didn’t want @fredsmith27182 to get the wrong idea :slight_smile: And I see your point, the return value on the shift doesn’t really come into play here anyway.

1 Like

No worries, you pointing that out educates us all.

1 Like

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?

oh yes i got it now. such a habit because i see the pattern of using arr.length for everything.

1 Like

Ya, you’re not alone, it catches the best of us :slight_smile:

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.

thank you, i was thinking the same thing to.

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.

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