How to know the highest value of an Array?

Asked

Viewed 28,233 times

7

I am making 1 calculator that adds values typed by the user and when the values of sums arrive in 1000 or exceed, the loop ends.

So far so good, the problem is to be able to identify the largest value typed by the user

Each time the loop rotates I put to put the current value of var = i in an array so that all values entered by the user are stored in it.

In the end all values will be inside the array, but how to find the largest one?

  • 3

    If any answer solved your problem, accept it. "Acceptance of a reply means that you received a response that worked...". You have 6 questions and have not marked any - you may have forgotten or not understood how the OS works, take a look at DOC.

4 answers

23

In JS this is very simple:

Math.max.apply(null, meuArray );


Example of use:

var meuArray= [0,12,13,2,1273,28,1];
var maior = Math.max.apply(null, meuArray );

document.getElementById( 'resultado' ).innerHTML = maior;
<div id="resultado"></div>

The apply works as if you have passed the values of array as function parameters max, and would amount to typing Math.max(0,12,13,2.... The first parameter equals the scope to be used in the function, and in this case, as is indifferent, we pass null, representing the global scope.

If your array has much more than one million items, which is usually not the case, you can use a solution with loop.


Alternative solution: the loop. (teaching example)

var maior = 0;
for (var i = 0; i < meuArray.length; i++) {
   if ( meuArray[i] > maior ) {
      maior = meuArray[i];
   }
}
  • How to use apply to the Math object if apply does not belong to the Math object?

  • @ropbla9 THE apply is method of function.

  • So, n is from Math :/

  • 2

    @ropbla9 everything that is Function "inherits" the apply (otherwise the demo would not work :) ).

  • By the way, which is good, because apply serves for practically everything that uses various parameters, and very useful to create situations dynamically.

  • Ah, I get it. I need to better understand these concepts of inheritance. Recommends some link?

  • @ropbla9 I usually search on MDN even.

Show 2 more comments

7

When I need to know the largest number of an array then I do one .sort().

For example:

[4,3,6,9].sort(function(a, b){return b - a;})

returns [9, 6, 4, 3], then I just need to get the first element. That is to say:

var elMaximo = [4,3,6,9].sort(function(a, b){return b - a;})[0];
  • what if it’s an array of objects and I want to sort by one of the attributes? array[{id: 1, name: 'Joao'}, {id: 2, name: Maria}], how would this sort by id?

  • @i9oni9on instead of return b - a would be return b.id - a.id

6

If there is already a cycle that occurs for the reception of the entered numbers, I believe that you could also solve the problem of determining the largest number by creating a variable that always stores the largest value of the typed numbers.

Example (assuming you’re using a do while):

    ...
    var maior = 0;
    do
    {
       if (numeroInserido > maior ) {
          maior = numeroInserido;
       }    
       // aqui podes vai o código responsável pela soma e restante validação do limite
       // máximo do valor 1000        
    }while (...)

In this solution it will not be necessary to store the values in a array to determine the largest number typed.

Note: I assumed that you save the numbers in an array so that you can later determine the largest of them.

  • If still, the preference is to keep the values in the array, you can do so in the same cycle, without using two.

  • This is the method I like. The best way to find something is to not lose it for the first time.

3

To find the largest number among a set of variables you can use the Math max function:

var a = 10;
var b = 25;
var c = 20;
maior = Math.max(a,b,c); 
console.log(maior) //25

It accepts multiple arguments so you can use spread (...) to use with arrays

var numeros = [1,2,5,200,89,5,50];
var maior = Math.max(...numeros);
console.log(maior) //200

as the name says, it "spreads" the array turning it into several separate arguments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.