QsainBolt
QsainBolt

Reputation: 15

Finding max value in array (array.find)

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.

Upvotes: 0

Views: 213

Answers (5)

Nina Scholz
Nina Scholz

Reputation: 386520

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);

Upvotes: 1

Michael Xu
Michael Xu

Reputation: 424

const scores = [10, 20, 30, 22, 25, 109, 90];

scores.reduce(function(a,b) { return a > b ? a : b });
// 109

Upvotes: 0

Midhun Krishna
Midhun Krishna

Reputation: 1759

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;
}

Upvotes: 0

Bartłomiej Mogielski
Bartłomiej Mogielski

Reputation: 11

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.

Upvotes: 0

brk
brk

Reputation: 50291

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)

Upvotes: 0

Related Questions