Show the largest and smallest number

Asked

Viewed 14,587 times

3

Why are you wrong?

var n1 = parseFloat(prompt("Digite um número:"));
var n2 = parseFloat(prompt("Digite um número:"));
var n3 = parseFloat(prompt("Digite um número:"));
function maiorDosTres(n1, n2, n3) {
  if ( n1 > n2 > n3) {
    alert( "O maior número é: " + n1 + " e o menor é: " + n3);
  } else if ( n1 > n3 > n2) {
    alert( "O maior número é: " + n1 + " e o menor é: " + n2);
  } else if ( n2 > n1 > n3) {
    alert( "O maior número é: " + n2 + " e o menor é: " + n3);
  } else if ( n2 > n3 > n1) {
    alert( "O maior número é: " + n2 + " e o menor é: " + n1);
  } else if ( n3 > n1 > n2) {
    alert( "O maior número é: " + n3 + " e o menor é: " + n2);
  } else if ( n3 > n2 > n1) {
    alert( "O maior número é: " + n3 + " e o menor é: " + n1);
  }
}
maiorDosTres(n1, n2, n3);
  • 1

    They have already implemented Math.max(args) and Math.min(args)

  • 1

    I cannot use functions. And the problem is that when I run, instead of showing the string with the highest and lowest number, it shows Undefined.

  • Has the operators && and || that can help you solve this. Something is missing from your :P comparisons

  • Thank you. It helped a lot.

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

6 answers

11

It is wrong because you cannot compare more than two operands at the same time. This does not exist:

if ( n1 > n2 > n3) {

The right thing would be:

if ( n1 > n2 && n2 > n3) {

This way you have three expressions with two operands each: n1 > n2, n2 > n3 and the relational operation && that has as operandos the results of the previous expressions.

The same goes for the others. I think you’ve got the hang of it.

Have better ways of doing this. Use Math.max() and Math.min() would be one of them. I will not put because it seems to me that you are doing this for study purposes and do not want the best solution.

10

You can use the functions Math.max() and Math.min(). The two functions receive all values for comparison as parameters. Although the question is why it is wrong, this form will make your code more streamlined.

var n1 = 10;
var n2 = 5;
var n3 = 7;

var max = Math.max(n1, n2, n3);
var min = Math.min(n1, n2, n3);

var pre = document.body.appendChild(document.createElement('pre'));
pre.textContent = 'dos valores ' + n1 + ', ' + n2 + ' e ' + n3;
pre.textContent += ' o máximo é ' + max + ' e o mínimo é ' + min;

If the dice are in one array utilize apply():

var numeros = [4,6,2,7,9,5];
Math.max.apply(Math, numeros);
  • 3

    Math.max() and Math.min() is the semantically correct/native way to compare values. +1

1

Save the values in an array and use the "for" for this case, the function becomes more flexible and serves for any number of arguments:

var n = [1, 2, 3];


numero=n(i)

for (i = 0; i < n.length; i++) { 
    if (n(i) >= numero) { maior = n(i); }
    if (n(i) <= numero) { menor = n(i); }
}
alert( "O maior número é: " + maior + " e o menor é: " + menor);
  • Could you explain your code? I was curious about how it works.

1

Why not ordain the numbers?

function maiorDosTres() {
    var a = Array.prototype.sort.call(arguments);
    alert( "O maior número é: " + a[a.length - 1] + " e o menor é: " + a[0]);
}

Fiddle

-3

Function maxEMim(n1, N2, N3){

var max = Math.max(n1,n2,n3);
var mim = Math.min(n1,n2,n3);

console.log(`Maior: ${max} e o menor: ${mim}`);

}

maxEMim(10,5,0);

-3

Let n1 = 1; Let N2 = 5; Let N3 = 7;

Let max = Math.max(n1,N2,N3); Let mim = Math.min(n1,N2,N3);

console log.(Maior: ${max} e o menor: ${mim});

Browser other questions tagged

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