Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

150
Vistas
Tell me which value from Math.max was the highest

I am using the following code to return the highest value out of a, b, c.

It does so successfully, but ontop of returning the highest value (which is a number) I would also like this to tell me WHICH variable returned the highest value. For example that variable "c" had the highest value.

let a = 1
let b = 2
let c = 3

let maxReturn = Math.max(a, b, c);
console.log(maxReturn);

This will return 3 as the result, which is what I need, but how do I also output the actual variable that had the highest value? In this case tell me: "Highest value is 3, found in C"

Thank you!!

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

With variables, it is very hard to be able to know what variable lines up with a,b,c without coding statements that will check the variables and see if it matches.

Using a format you can loop over means you have the ability to easily reference.

const getMax = (data) => Object.entries(data).reduce((max, item) => max[1] > item[1] ? max : item);

var myData1 = { a: 1, b:2, c: 3};
var myData2 = { a: 4, b:2, c: 3};

console.log(getMax(myData1));
console.log(getMax(myData2));

about 4 years ago · Juan Pablo Isaza Denunciar

0

see second example for a better way. remember indices are 0 based

let a = 1
let b = 2
let c = 3

let maxReturn = Math.max(a, b, c);
if (maxReturn == 1)console.log(maxReturn,"a");
else if(maxReturn == 2)console.log(maxReturn,"b");
else if(maxReturn == 3)console.log(maxReturn,"c");



let x = [1,2,3]

maxReturn= Math.max(...x)
let index = x.indexOf(maxReturn)
console.log(maxReturn,index)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda