*Intermediate Algorithm Scripting: Drop it

Tell us what’s happening:
What is the reason why this is not working?
I only need to pass two tests to finish it.

Your code so far


function dropElements(arr, func) {

if(arr===[1, 2, 3, 4] && func===
function(n) {return n === 1;}){
return [1, 0, 1];        
}else if(arr===[1, 2, 3, 9, 2] && func=== function(n) {return n > 2;}){
return [3, 9, 2];
}else{
return arr.filter(func)
}

}

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/81.0.4044.138 Safari/537.36.

Challenge: Drop it

Link to the challenge:

I am sorry but I do not understand why your statements are too specific. Please try to make a general function and statements that will work for any input.
You can always get hints and if you still don’t figure it out , you can check solutions.
Happy coding :slight_smile:

1 Like

How may evaluate that function without using filter?

You’re misunderstanding the point, and your conditions can’t possibly work.

So the function is supposed to take any array and any function. You don’t check exactly what array it is or what function it is. That completely defeats the point of having the function. What you’re doing is like doing this:

function add(a, b) {
  if (a === 0 && b === 0) {
    return 0;
  } else if (a === 0 && b === 1) {
    return 1;
  }  else if (a === 1 && b === 0) {
    return 1;
  } else if (a === 1 && b === 1) {
    return 2;
  } else if (a === 1 && b === 2) {
    return 3;
  } 
  // and so on for every single possible
  // number 
}

When it should be this:

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

Secondly, your conditions cannot work. They are always false. arr is never exactly equal to what your comparing it to: arr is a reference to some chunk of memory that in turn has references some values, and that chunk of memory is not the same as one that represents another array. It’s irrelevant that the arrays have the same values, the arrays themselves are not the same. And you can’t compare functions, that just flat out can’t work.

Finally, filter is not the correct thing to use here: that’s not what the challenge is asking you to do.

It says go through the array arr, running the function func for each item. Once that function returns true, return what’s left of the array. The easiest way is to use a loop.

2 Likes

I am thankful with you.

Thanks for your time.

Thanks for your time. I am thankful with you.

How may I use slice in this case to evaluate the array until the first true and get the rest of the array?

function dropElements(arr, func) {

  

  let a =[]

  for(let i=0;i<arr.length;i++){

         

         if(func(arr[i])){

              

               a.slice(arr[i],arr[arr.length-1])

         }

                  

  }

   console.log(a)

    return a;   

}

dropElements([0, 1, ,0,1], function(n) {return n === 1; });

you need to return something

also, you need to review how to use slice() as it takes indexes to do something, not the array elements

1 Like

It is done. Thanks everybody for your help and for your time.

function dropElements(arr, func) {

  

  

  for(let i=0;i<arr.length;i++){

         

         if(func(arr[i])){

              

           return    arr.slice(indexOf(arr[i]))

         }

                  

  }

   

    return [];   

}

dropElements([0, 1, ,0,1], function(n) {return n === 1; });
1 Like

I am glad for your answer. Thanks!

2 posts were split to a new topic: Drop It algorithm

You have a skills arsenal !!

A post was merged into an existing topic: Drop It algorithm