Printing the highest value variable

Asked

Viewed 85 times

-2

I’m trying to print the variable with the highest value, but the value is always N1. How do I print the correct value?

var n1 = 4;
var n2 = 7;
var n3 = 6;

if (n1 > n2, n3) {
console.log("N1 é a maior variável")
} else if (n2 > n1, n3) {
console.log("N2 é a maior variável")
} else{
console.log("N3 é a maior variável")
};
  • You know logic operators ? take a look at the documentation https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operadores_Logicos

  • i don’t know anyone, but in this example I quoted above, so I won’t use if?

  • It is not possible to know the purpose of the code, but normally, in situations like this, you will not use an algorithm that simply compares pre-initialized values, because in practice it would not make sense. The best way to make the code more usable is to create a higher variableValor, start it with zero (in case it contains only positive numbers) and perform the comparisons. At the end, just print the highest variable.

  • is that are exercises to learn how to work javascript, mass brigadea

4 answers

2

Use that:

var n1 = 4;
var n2 = 7;
var n3 = 6;
        
if (n1 > n2 && n1>n3) {
  console.log("N1 é a maior variável")
} else if (n2 > n1 && n2>n3) {
  console.log("N2 é a maior variável")
} else{
  console.log("N3 é a maior variável")
};

1

It follows another alternative, but it would be necessary to change its input structure to instead of variables mount an object and use them as properties of it.

//convertendo a sua entrada em um objeto
var entrada = {
  N1: 4,
  N2: 7,
  N3: 6
}

var imprimeMaior = function(input) {
  //criando a estrutura para armazenar e comparar o maior valor 
  var maior = {
    nome: null,
    valor: null
  };

  //percorrendo todos as propriedades do objeto de entrada
  for (item in input) {
    //se for maior ele armazena
    if (input[item] > maior.valor) {
      maior.nome = item;
      maior.valor = input[item];

    }
  }
  console.log(maior.nome + " é a maior variável");

}

//executando o método
imprimeMaior(entrada);

0

To avoid multiple ifs elses if there are too many values to test index the library Underscore.js

Underscore.js is a small library of code utilities focused primarily on data structure manipulation.

For measly 14kb you get features like map, select and invoke, plus a templating engine that, by itself, already makes use of the library.

Another nice point is that scripts always seek to use native browser resources, that is, in modern browsers the library takes advantage of the implementations of foreach, map, index, filter etc.

var n1 = 4;
var n2 = 7;
var n3 = 6;
var n4 = 8;
var n5 = 3;
var n6 = 5;
var n7 = 2;
var n8 = 1;

var obj = {
    n1: n1,
    n2: n2,
    n3: n3,
    n4: n4,
    n5: n5,
    n6: n6,
    n7: n7,
    n8: n8

};

var maior = _.max(Object.keys(obj), function (o) {
    return obj[o];
});


console.log(maior);
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js"></script>

  • But then it would not return the name of the variable with higher value

  • 1

    @Leandroangelo, ops, was wrong, I got it wrong. I’m going to present a very nice library for this and other things

-1


Using the basis that the colleagues above sent I have put together something a little more descriptive, maybe it will be useful for you in this beginning.

To help I will try to explain in a simple way the about 2 logical operators that are most used in the beginning the || and the &&.

The || is a logical operator that translated means 'OR', or in English 'OR'. It is used when at least one of its conditions is true.

The && is a logical operator that translated means 'AND', or in English 'E'. It is used when all its conditions are true.

In his case it was used the && therefore it was necessary that the tested value was bigger than all the others at the same time.

I hope I helped. I’ll leave one link that can be useful too.

  //Declaração das variáveis.
  var valor1 = 3;
  var valor2 = 9;
  var valor3 = 1;

  //Condições para encontrar o maior valor.

  //Caso o valor1 seja maior que o valor2 E valor1 seja maior que valor3 (O valor1 é o maior).
  if (valor1 > valor2 && valor1 > valor3) {
      console.log("O valor 1 é o maior")
  } 
  //Caso o valor2 seja maior que o valor1 E valor2 seja maior que valor3 (O valor2 é o maior).
  else if (valor2 > valor1 && valor2 > valor3) {
      console.log("O Valor 2 é o maior")
  } 
  //Caso não seja o valor1 nem o valor2, então quer dizer que o valor3 é o maior.
  else {
      console.log("O valor 3 é o maior")
  };
  • @Viniciusmacedo, although the author’s doubt is related to operators, which would have other duplicates, as this response would be useful to other users if considering that n1,N2 and N3, in fact, represent `N'?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.