2
How do I locate the position of the largest and smallest element in a Javascript array?
With the reduce
I was able to locate the largest and smallest element, but I need to get the position (index) also from them inside the array.
Greater number:
var array = ["885.9", "984.9", "444.9", "528.9", "528.9"];
var maior_numero = array.map(Number).reduce(function(numero1, numero2) {
return Math.max(numero1, numero2);
});
console.log(maior_numero );
Fewest:
var array = ["88", "999", "12", "123", "230"];
var menor_numero = array.map(Number).reduce(function(numero1, numero2) {
return Math.min(numero1, numero2);
});
console.log(menor_numero );
I think the
reduce
then it’s kind of unnecessary. You could have done something likeMath.max.apply(null, ["885.9", "984.9", "444.9", "528.9", "528.9"])
– Wallace Maxters
Thanks Wallace for the feedback, so this code you helped me with is taking the value of the element. Besides the precise pick value in which position it is in the array, it could help me?
– JJB