How to find the largest in the cycle for

-1

document.getElementById("comparable").addEventListener("click", mayor);

function mayor() {
  var numeros = [],
    ingresar = prompt('Ingresa los números, separados por comas', ''),
    j = 0;
  var c = ingresar.replace(new RegExp(',', "gi"), " ").split(" "),
    var v = c.length;
  for (; j < v; j++) {

  }
}
<input type="button" id="comparable" value="Comparar" />

So far, I receive the numbers in función , I remove the coma and I put them in array . Then my idea is recorrerlo and the ** largest of all will be returned , but how do I verify the largest one in a cycle for ?

    
asked by Eduardo Sebastian 18.09.2017 в 01:23
source

6 answers

1

Well, although you do not specify it, I suppose your function will have to do something with the major number , for example return it ; in such case:

function mayor() {
  return Math.max(...prompt('Ingresa los números, separados por comas','').split(','));
}

Although it is not a good idea to group everything in one line for reasons of readability this time I have written it that way because of the simplicity of the code, although the truth should have been collected first by the user's response in a variable and then provide them to the static feature of Math

    
answered by 18.09.2017 / 03:11
source
2

To obtain the largest number of an array, it is no longer mandatory to write code that iterates over the array!

For that there is Math.max() . Having an array of numbers, you can get the largest of them two ways.

I. With the propagation operator (spread operator)

With the new spread operator (ECMA Script 2015), get the maximum of an arrangement becomes much easier.

var arr = [1, 2, 3];
var max = Math.max(...arr);

II. With Function.prototype.apply() : recommended for large arrays

The following function uses Function.prototype.apply() to find the largest element in a numeric array. getMaxOfArray([1, 2, 3]) is equivalent to Math.max(1, 2, 3) , but getMaxOfArray() can be used on programmatically constructed arrays of any size.

function getMaxOfArray(numArray) {
  return Math.max.apply(null, numArray);
}

Code:

//Definimos un array para probar
var numArray = [1, 2, 3];

//Forma I
var max = Math.max(...numArray);
console.log(max);


//Forma II
var mayor=getMaxOfArray(numArray);
console.log(mayor);

function getMaxOfArray(numArray) {
  return Math.max.apply(null, numArray);
}

See: Math.max in the MDN documentation

    
answered by 18.09.2017 в 01:47
0

try this:

function mayor(
var max = 0;
var array = [];
    for (var i = 0; i < array.length; i++) {
        if (array[i] > max) {
            max = array[i];
        }
    }
    alert(max);
)
    
answered by 18.09.2017 в 01:40
0

my answer will not be in javascript, but I can do it in pseudocode, and you can extrapolate it in Javascript.

Grace, is to keep a variable that contains the highest value at all times while the loop is traversed. While the loop is traversed, the new value is compared with said variable, and if it is greater, it is substituted, otherwise it is simply advanced. At the end of the loop you will have the highest value stored.

variable  numMaximo = -1;

for(int i = 0; i < arrayARecorrer.size() ; i++){
    si (arrayARecorrer[i]>numMaximo){
         numMaximo = arrayARecorrer[i];
    }
}
    
answered by 18.09.2017 в 01:41
0

Regards

After

var c = ingresar.replace(new RegExp(',', "gi"), " ").split(" "),var v = c.length;

you create a variable where you keep the largest one; Ahem:

var elMayor = Number(c[0]); /* Asumo que el array al menos tendrá un elemento y tomamos el primer elemento como el mayor */

Of course it is also that the present considers that the array obtained from the Split has at least one element; and all correspond to number; therefore, I did not include verification or validation of the data before comparing it. In your for :

for(j=0;j<v;j++){
  if (elMayor<Number(c[j]))
    elMayor=c[j];
}

alert(elMayor);

May it be useful to you.

    
answered by 18.09.2017 в 01:46
0

you could do this:

function mayor(){
    var mayor,
          ingresados = prompt('Ingresa los números, separados por comas', '');

    ingresados.split(',');

    mayor = Math.max.apply(null, ingresados);
    return mayor;
}

The function returns the largest number of the Array, I hope and it serves you.

    
answered by 18.09.2017 в 02:19