-6

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
  • 580
  • 1
  • 5
  • 14
  • 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

2 Answers2

2

As stated above, this is pretty simple.

Math.max(AB, ZM, TP, NN, LW);    
react_or_angluar
  • 1,568
  • 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
0

@JakeKempo. I saw your question that is the same that I had today and I figured out one solution.

code in VScode

Code below.

var compra, maiorQtd = 0, itemMaiorQtd = 0, open = true, qtd = 0, hort = 0, lat = 0, car = 0, pei = 0, ave = 0, array = [], itens = ["Hortifruti", "Laticícios", "Carnes", "Peixes", "Aves"];

while (open == true) {
  compra = parseFloat(
    window.prompt(
      "Digite o número referente à sua compra conforme abaixo: \n\n(1) Hortifruti \n(2) Laticínios \n(3) Carnes \n(4) Peixes \n(5) Aves \n(6) Fechar pedido"
    )
  );
  //Condição para não aceitar outros valores além dos que estão na tela
  if (compra >= 1 && compra < 6) {
    qtd = parseInt(window.prompt("Digite a quantidade"));
  } else if (compra == 6) {
  } else {
    window.alert("Este valor não existe. Digite um dos valores da lista.");
  }
  switch (compra) {
    case 1:
      hort += qtd;
      break;
    case 2:
      lat += qtd;
      break;
    case 3:
      car += qtd;
      break;
    case 4:
      pei += qtd;
      break;
    case 5:
      ave += qtd;
      break;
    case 6:
      array.push(hort, lat, car, pei, ave);
      //Verificar qual é o ítem de maior quantidade
      for (i = 0; i < 5; i++) {
        if (maiorQtd < array[i]) {
          maiorQtd = array[i];
          itemMaiorQtd = itens[i];
        }
      }
      window.alert(
        `Compra finalizada. O ítem de maior quantidade nesta compra foi o ${itemMaiorQtd} com ${maiorQtd} unidades.`
      );
      open = false;
  }
}
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/32214682) – mattsahr Jul 16 '22 at 05:55