Answer the question
In order to leave comments, you need to log in
How to replace for with .reduce?
I'm trying to reduce the number of lines and even got stuck. There is such code:
for (let i = 0; i < rClon.length; i++) {
if (rMax === rClon[i]) {
rClon.splice(i, 1);
break;
}
} I
tried to remake it into this and at the moment with the break it gives an error
rClon.reduce((rNone, i1) => rMax === i ? rClon.splice(i1, 1); break;);
I don’t understand how to write it down,
the loop should find the first element of rMax in rClon, delete it and break
, maybe it’s not necessary to use rudus here?
Answer the question
In order to leave comments, you need to log in
Methods .forEach, .map, .reduice, etc. not interrupted.
.indexOf is enough here
const idx = rClon.indexOf(rMax);
if (idx !== -1) {
rClon.splice(idx, 1);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question