1
I wanted to know how I got the last element of array using the structure for
in the case of decreasing?
This is my code:
const a = n => n % 2 === 0;
function callbackfn (valorInserido, funcao) {
for (valorInserido.length; i=0; i--) {
if (funcao(valorInserido[i])) {
return valorInserido[i];
}
}
}
console.log(callbackfn([3,5,2,4],(a)));
In case it was to return the number 4
, but returns undefined
.
This code has syntax errors and does not seem to do what is described in the question. You can clarify?
– Maniero
@Maniero it is a code that has the same function as the
find
however, I want to do afindLast
and I’m not getting it. I just want to make it take the last value that returns true. ofind
takes the first value that returns true, I want to take the last.– Jota
You don’t need
for
usesarray[array.length - 1]
that will return the last element– Pedro Pinto
@Pedropinto he wants conditionally
– Maniero