-5

I have a list of variables, I want to find the largest: AB, ZM, TP, NN or LW using javascript (or jquery)

let AB = MGD+LMGF*1.004;
let ZM = MBR+CRR*1.003;
let TP = CBS+DBK*1.005;
let NN = EEF+SSD*1.001;
let LW = WLB+CCT*1.002;
JakeKempo
  • 552
  • 1
  • 5
  • 13
  • 1
    Have you researched anything? There’s a function specifically for finding the maximum value. Don’t ask here if you haven’t tried anything. – Sebastian Simon Oct 25 '16 at 02:00
  • Ever heard of `Math.max()`? – StackSlave Oct 25 '16 at 02:01
  • Possible duplicate of [Finding highest value(s) amongst JavaScript variables](http://stackoverflow.com/questions/16095301/finding-highest-values-amongst-javascript-variables). Also [How can I use JavaScript to get the highest value of two variables](http://stackoverflow.com/q/12054897/4642212) and keep in mind, `Math.max` can take more than two arguments. – Sebastian Simon Oct 25 '16 at 02:02
  • *"The result should be the name of the variable that has the highest value "* - What do you plan to use the name of the variable for? Sounds like you may have an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – nnnnnn Oct 25 '16 at 02:46

1 Answers1

1

As stated above, this is pretty simple.

Math.max(AB, ZM, TP, NN, LW);    
react_or_angluar
  • 1,557
  • 2
  • 13
  • 20
  • I tried that but it just gives me the highest value – JakeKempo Oct 25 '16 at 02:07
  • @JakeKempo how is it different from what you asked? – zerkms Oct 25 '16 at 02:08
  • I want to know AB, ZM, TP, NN or LW, not a number – JakeKempo Oct 25 '16 at 02:09
  • What type your result should be? A string? – zerkms Oct 25 '16 at 02:09
  • The result should be the name of the variable that has the highest value – JakeKempo Oct 25 '16 at 02:11
  • @JakeKempo See: http://stackoverflow.com/q/4602141/5647260 - What you're trying to do is a bit impractical... why do you need such thing? What if the values passed in aren't variables? How would you know? – Andrew Li Oct 25 '16 at 02:13
  • @JakeKempo what would you do with the variable name next? Do you realise that you won't be able to refer the variable with just its name as a string? – zerkms Oct 25 '16 at 02:13
  • 1
    `const o = { AB, ZM, TP, NN, LW }; const name = Object.entries(o).reduce((m, c) => m[1] > c[1] ? m: c)[0];`. But as I mentioned - just having a name in a string makes very little sense. – zerkms Oct 25 '16 at 02:15