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