[Solved] Drop it, I think my answer is correct but it doesn't work on FCC

Tell us what’s happening:
Okay, so I do these problems in VS Code, then put the answer into FCC. I don’t know if THAT’s the issue, but when I run my code in the browser from VS Code, I get all the right answers for the tests. But when I tried to run it in FCC, it says all the tests failed.

I understand that my way of doing this might be more complicated than it needed to be, but it was the first thing that came to mind.

Here is a JSFiddle of what mine does, and it works in the JSFiddle when you look at the Console.

I’m just wondering why the FCC tests are saying it failed when I am pretty sure I have a correct answer.

Your code so far


function dropElements(arr, func) {
  // Drop them elements.
  let toggle = false;
  for (let i = 0; i < arr.length; i++) {
    if (func(arr[i])) {
      console.log(arr.splice(i));
      toggle = true;
      return arr.splice(i);
    } else if (i === arr.length - 1 && !func(arr[i])) {
      console.log([]);
      arr.splice(arr.length);
      return [];
    }
  }
}

dropElements([1, 2, 3, 4], function(n) {
  return n >= 3;
}); // [3, 4]

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

dropElements([1, 2, 3], function(n) {
  return n > 0;
}); // [1, 2, 3]

dropElements([1, 2, 3, 4], function(n) {
  return n > 5;
}); // []

dropElements([1, 2, 3, 7, 4], function(n) {
  return n > 3;
}); // [7, 4]

dropElements([1, 2, 3, 9, 2], function(n) {
  return n > 2;
}); // [3, 9, 2]

Your browser information:

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

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

      console.log(arr.splice(i));
      toggle = true;
      return arr.splice(i);

splice() modifies the array. You are calling it twice, so the array that you are returning is not the same as the one that your console prints two lines earlier.

Oh jeez. That fixed it. I guess I assumed that since it was a console.log, that since the way I called it twice, it would just be the same thing, not splice it twice. Thank you! I took the console.log out and tried it again and it worked just fine.

If you want to both return and console log something you need to assign it to a variable

let a = arr.splice(i);
console.log(a);
return a;