Js drop it challenge

Tell us what’s happening:
My code works very well for the other arguments apart from the one provided below. i want to make it work. please help

  **Your code so far**

function dropElements(arr, func) {
var array=[];
array.push(arr.filter(func))
  console.log(array)
return arr.slice(arr.indexOf(array[0][0]), arr.length)
}

console.log(dropElements([1, 2, 3], function(n) {return n > 3; }));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Linux; U; Android 7.0; TECNO P701 Build/NRD90M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/88.0.4324.181 Mobile Safari/537.36 OPR/54.0.2254.56148.

Challenge: Drop it

Link to the challenge:

Why push the result of the filter into an array instead of just a normal variable?

There’s an array method that will find the first element that matches some condition, by the way.

I used an if… else statement and it didn’t work

it won’t affect the outcome, will it?

Probably not, but it makes it much harder to read and debug your logic.

function addTwo(a, b) {
  return a + b;
}

function addTwoComplex(a, b) {
  let values = [[a], [b]];
  return values.reduce((sum, val) => val[0] + sum, 0);
}

Both of these functions add together a and b. The second is unnecessarily complex and hard to reason about.

i understand. infact the if…else syatement that refused to work, worked perfectly when i removed the push() syntax.

thank you everyone, this is how my code look like and it works. every contribution help.

function dropElements(arr, func) {
  var array = arr.filter(func);
  if (array == 0) { // what happens if you use !array here instead of array == 0?
    return []
  } else {
    return arr.slice(arr.indexOf(array[0]), arr.length)
  }
}

Nice work! I added one small comment for you to consider.

1 Like

It works fine. Thank you @JeremyLT

1 Like

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