Intermediate Algorithm Scripting - Drop it

Right! You can use i to slice but you can’t use n

im still not catching that first value?

function dropElements(arr, func) {
  for(let i = 0; i < arr.length; i += 1){
    let n = arr[i];
    if (func(n)){
      arr = arr.slice(i)
    }

  }

  return arr;
}

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

its working for the second func call but leaves arr[0] out on the first?

You’re violating this rule again though

You end up with a confusing mess if you change arr at all in the middle of the loop.


Do you need to keep looking after you found the right index i?


I’d use

    const elem = arr[i];
    if (func(elem)){

i is one of the only single letter variable names I’d ever use!

so break? like after true just break… then mutate the arr?

Never mutate arr! Let’s say it together now ‘never mutate arr’!

You don’t need to mutate arr. You can break, that would stop the loop, though then you need to get the index i outside of the loop. There’s another way to stop a loop (and the whole function) that you used in some of your code earlier in this thread.

can you like quote the whole bit in the thread where that might have happened? :innocent: :slightly_smiling_face: :upside_down_face: :melting_face:

unless you meant return

function dropElements(arr, func) {
  for(let i = 0; i < arr.length; i += 1){
    let elem = arr[i];
    if (func(elem)){
      return arr.slice(i)
      
    }

  }

 return []
}

console.log(dropElements([1, 2, 3], function(n) {return n < 3; }));
console.log(dropElements([1, 2, 3, 4], function(n) {return n >= 3;}))
console.log(dropElements([1, 2, 3, 4], function(n) {return n > 5;}))

thanks… very much

Good work getting it passing :tada: