Tell us what’s happening:
My code executes 5/6 requirements except for the output below. Can’t seems to figure out what’s wrong, tried adding else {return }. Got that checked but the rest not. Hmmmm…
Your code so far
function dropElements(arr, func) {
var x = [];
for (let i = 0; i < arr.length; i++) {
if (func(arr[i])) {
x.push(arr.indexOf(arr[i]));
if (x.length === 1) {return arr.slice(x)}
}
}
};
console.log(dropElements([1, 2, 3, 7, 4], 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/80.0.3987.132 Safari/537.36.
Your idea with returning is pretty close, only thing is, if you do this with an else, it gets returned to early, the loop stops and not all numbers get checked …
var x = [];
for (let i = 0; i < arr.length; i++) {
if (func(arr[i])) {
x.push(arr.indexOf(arr[i]));
if (x.length === 1) {return arr.slice(x)}
}
}
if (x.length === 0) {
return [];
}
};