0

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.

asked May 5, 2020 at 11:54
3
  • 1
    you need to iterate over all items, but find has a short circuit by returning true, then it take the actual element as value. the main question, why do you want to take find and the second question how do you like to avoid a second loop? Commented May 5, 2020 at 12:03
  • 3
    It is a poor choice to employ 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? Commented May 5, 2020 at 12:09
  • A sensible find function would already know the max value, thus it's completely superfluous. Commented May 5, 2020 at 12:09

5 Answers 5

1

You could take a closure over an index for looping from the end and a temporary max value which is at start undefined and gets the first value from the first element.

Then loop while the value at temp index is smaller than score, store this value in max, repeat.

At the end return the result if index plus one is equal to the temp index.

This approach takes a single loop. find iterates from start of the array and the inner loop from the end of the array if both indices cross, the result is found.

const
 scores = [100, 20, 30, 22, 25, 109, 90],
 maxScore = scores.find(
 ((j, max) => (score, i, array) => {
 if (max === undefined) {
 max = score;
 j = array.length;
 }
 if (score < max) return;
 while (array[j - 1] < score) max = array[--j];
 return i + 1 === j;
 })
 ()
 );
console.log(maxScore);

answered May 5, 2020 at 12:25
Sign up to request clarification or add additional context in comments.

Comments

0

The simplest way to do it, without using any Array methods, can be written as:

const maxScore = (scores) => {
 let score = 0;
 for ( let i = 0; i < scores.length; i++ ) {
 if(scores[i] > score) {
 score = scores[i]
 }
 }
 return score;
}

From MDN:

The find() method returns the value of the first element 
in the provided array that satisfies the provided testing function.

Lets redefine our simple function again,

const maxScore = scores => {
 let score = Number.NEGATIVE_INFINITY;
 scores.forEach(element => {
 let acc = scores.find(number => number > score);
 if(!isNaN(acc)) {
 score = acc;
 }
 })
 return score;
}
answered May 5, 2020 at 12:22

Comments

0

find works on each array element. So take the max outside the find method & log max. Besides there were two typos

const scores = [10, 20, 30, 22, 25, 109, 90];
let max = 0;
const maxScore = scores.find((score) => {
 for (let i = 1; i < scores.length; i++) {
 if (scores[i] > max) {
 max = scores[i];
 };
 };
 return max;
});
console.log(max)

answered May 5, 2020 at 12:05

Comments

0

Try this:

const scores = [10, 20, 30, 22, 25, 109, 90];
let max = 0;
scores.find(score => { if(score > max) max = score });
console.log(max);

Your current code is looping the scores array whilst its already looping it, JavaScripts .find, essentially loops the array.

answered May 5, 2020 at 12:06

2 Comments

find is not necessary here, forEach would work too.
of course, but OP said he needs to to use the find function.
0
const scores = [10, 20, 30, 22, 25, 109, 90];
scores.reduce(function(a,b) { return a > b ? a : b });
// 109
answered May 5, 2020 at 13:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.