Drop it. console.log() logs right results, but it doesn't pass

Drop it. console.log() logs right results, but it doesn't pass
0

#1

This code doesn’t pass the challenge. Is it because of low efficiency?
Because when I console.log() the challenges, they log the right results.
Here are the results:
1 = ,3,4

2 = ,1,0,1

3 = ,1,2,3

4 = ,

5 = ,7,4

6 = ,3,9,2


  function dropElements(arr, func) {
      // Drop them elements.
      let savedNumbers = [];
      let allTrue = arr.every(x => func(x));
      
      for (var i = 0; i < arr.length; i++) {
        if (func(arr[i])) {
          savedNumbers.push(arr.slice(i));
          break;
        } else if (allTrue) {
          savedNumbers = [...arr];
        } else if (!allTrue) {
          savedNumbers.length = 0;
        } 
      }
      return savedNumbers;
    }  

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

    console.log('2 = ', dropElements([0, 1, 0, 1], function(n) {
      return n === 1;
    }));

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

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

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

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/drop-it/


#2

I think you’re relying on the FCC console. As a result, you’re not seeing the answer to your problem. Hit F12 on your browser, and you will get much more detailed output. Notice the exact nature of the arrays that are printing. Take a look and if you still don’t get it, check this out:

Whether that helps or not, let me know how it goes.


#3

Man! I feel stupid. I didn’t see that by pushing the result, I had the result array as en element of
an array. Although, I don’t know if just assigning it, is a good practice. I remember someone telling me to use push, because assignments are error prone. I don’t remember if it was a different case… Anyway thanks for the tip. I appreciate it. :slight_smile:


#4

Programming is about making the same stupid mistake a thousand times until one day you are a master.


#5

Well, pushing wasn’t your problem, it was the fact that you were slicing the array (which returns an array), and pushing that array, resulting in the nested construct. If you rewritten the relevant line to:

savedNumbers.push(arr[i])

You would have solved the problem and avoided assigning.

The possibilities for problems in assigning to arrays arise in 2 main ways:

  1. You could assign something else to the variable that references the array by mistake, like:
let arr  = [ 1, 2, 3 ];
arr = 4;
console.log(arr);

expected output: 4

  1. Alternatively, you could assign to indices that are out of range and lead to empty slots, like:
let arr = [1,2,3];
arr[2] = "A";
arr[4] = "B";
console.log(arr);

Expected output: [ 1, 2, "A", <1 empty slot>, "B" ]

There are of course other errors you can make, but they can be classified as typo and logic errors (arr[let i=2];) and the like.

That being said, you don’t need to be afraid of assigning to arrays if you test code before it gets too complicated. In fact, for some applications, it matters because dynamically resizing arrays is far slower than declaring an over-sized array and assigning to various indices manually. However, that’s a production-level decision to optimize speed. When you are working on the algorithm, use push() and the like because it’s easier to read, and easier to check the logic. You can always refactor later if performance is an issue, but at least you’ll have a logically-sound version from which to start.


#6

Thanks for the explanation about assigning to array etc. I appreciate it.
As about savedNumbers.push(arr[i])
it’s not giving the right results. I deleted also the break, but second and last tests don’t pass.


#7

So, what’s your current code?


#8

It’s the above code, with the difference that instead of savedNumbers.push(arr[i]) I do savedNumbers = arr.slice(i); and it passes all the tests :wink:


#9

Okay. I was a little confused when you said it wasn’t passing. Congrats! Mark my reply with the code as the solution, and keep coding!


#10

I did marked the one with the link for the slice() function.
Thanks again man.
Happy coding.
:wink: