Tell us what’s happening:
my code bring the right results. But freecodecamp is not accepting. whats wrong ?
Your code so far
function dropElements(arr, func) {
// Drop them elements.
var lst = [];
for (let i = 0; i < arr.length; i++) {
if (func(arr[i]) === true) {
lst.push(arr.splice(i));
}
}
return lst;
}
var result = dropElements([1, 2, 3, 9, 2], function(n) {return n > 2;});
console.log(result);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/drop-it
Actually, it is not returning the correct results.
For example, for the first test dropElements([1, 2, 3, 4], function(n) {return n >= 3;})
, your function returns [ [ 3, 4 ] ]
, but the correct answer is [ 3, 4 ]
. You are returning a multi-dimensional array instead of a one-dimensional array. Remember that arr.splice(i)
is still an array, so you are pushing an array into an array instead of the values of the array into an array.
I am wondering how i can know this next time. Because on the console it will bring it as [ 3, 4 ] instead of [ [ 3, 4 ] ] which you just showed me.
I see, you are extracting correct result and building new array, but it actually asks rest array after condition is true, suppose dropElements([1, 2, 3, 9, 2], function(n) {return n > 2;}); Here arr[2] > 2, so here condition is true, and from now, rest array is [3,9,2], But I think, there may be a some mistakes, it checks only first element and accepts the output.