Im learning Javascipt and actually im on episode with array methods. My imaginary exercise relies on found the Max/Min value in array by array.find method.
Acutally I did smth like that, but script returned me "Undefined". Please help. :)
const scores = [10, 20, 30, 22, 25, 109, 90];
const maxScore = scores.find(score => {
let max = 0;
for (let i=1; i < scores.length; i++){
if(score[i] > max){
max = score[i];
};
};
return max;
});
console.log(maxScore);
P.S. I know about "Math.max.apply", but I have to do it by array.find and simple loop.
true, then it take the actual element as value. the main question, why do you want to takefindand the second question how do you like to avoid a second loop?find()this way for variety of reasons: you don't benefit from its key feature (exiting loop upon first match) and furthermore, you do nested loop inside of it, so your implementation does O(n²)-time lookup which is unnecessarily slow, whereas single loop is pretty much enough. Would you elaborate a bit on the reasons to do so in order for us to suggest some suitable options?findfunction would already know the max value, thus it's completely superfluous.