Tell us what’s happening:
my code works for most cases, yet only one passes. what is wrong?
Your code so far
function dropElements(arr, func) {
// Drop them elements.
if(func(arr[0])) {
return arr;
} else {
arr.shift();
dropElements(arr,func);
}
}
dropElements([1, 2, 3, 7, 4], function(n) {return n > 3;})
//should return '[7, 4]' and it does, yet i don't pass
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36
.
Challenge: Drop it
Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/drop-it
Your else
condition doesn’t return anything.
it’s not supposed to.
it’s a recursive loop that drops the first element in the array, and starts the function again
UPDATE: i finished the code, it individually passes all the tests in the console, but the GUI still doesn’t let me pass
function dropElements(arr, func) {
// Drop them elements.
if(func(arr[0]) || arr.length === 0) {
console.log(arr);
return arr;
} else {
arr.shift();
dropElements(arr,func);
}
}
dropElements([1, 2, 3, 4], function(n) {return n > 5;})
ilenia
December 2, 2019, 10:27pm
6
then what do you do with the value returned from the call inside the else?
remember that if a function doesn’t return anything, it returns undefined by default. The else statement doesn’t contain a return
, so if the else run, nothing is returned