How do I get the second and last tests for the challenge to pass? I can’t figure it out. Would someone please me out here?
My code so far
function dropElements(arr, func) {
for (const num of arr) {
if (!func(num)) {
arr.splice(arr.indexOf(num), 1);
}
}
return arr;
}
console.log(dropElements([0, 1, 0, 1], function(n) {return n === 1; }));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36 Edg/84.0.522.40.
Currently your function removes all elements from the array that return false for the func parameter. Instead, the instructions want you to remove the false elements until you hit the first true element, then return that array.
do not mutate the array on which you are iterating… that doesn’t work well usually
there are simpler ways, and this still doesn’t work
do you understand the requirements?
with the [0, 1, 0, 1] array, the test is n === 1, that means that you need to return [1, 0, 1]
[0, 1, 0, 1]
|
this is the first element in the array that passes the test (returns true)
you need to return an array like this
but with everything before the passing element removed,
so you need to return [1, 0, 1]
I get what I need to return. What I’m not getting is the “how”.
This is what I have:
function dropElements(arr, func) {
let index = 0;
let resultArr = [...arr];
while (!func(arr[index])) {
resultArr = resultArr.splice(index, 1);
index++;
};
console.log(arr);
console.log(resultArr)
}
What I missing? resultArr is currently just 0 with that input. Is there a way I could combine the two arrays in such a way that the element that splice took out isn’t included in the resulting array?