简体   繁体   中英

What is wrong with my operators? Javascript

I do not know why it is not working. It is supposed that when you got a mark between 90 or more that equals A. When you got between 80 and 89 that equals B and so on...

let nota = prompt("Ingrese su nota: ");


if (nota >= 90){
  console.log(nota + " " + "equivale a una A");
}

else if (nota == 80  ||  nota <= 89){
  console.log(nota + " " + "equivale a una B");
}
else if (nota == 70  ||  nota <= 79){
  console.log(nota + " " + "equivale a una C");
}
else if (nota == 60  ||  nota <= 69){
  console.log(nota + " " + "equivale a una D");
}
else{
  console.log("Tienes una F");

}

Your problem is that in the second test:

nota == 80  ||  nota <= 89

every value of 89 or less will pass and so they'll all get a B. Note also that the test nota == 80 is redundant since 80 is also <= 89, so the test is equivalent to:

nota <= 89

Of course you could do:

nota >= 80  &&  nota <= 89

but that's more complex than it needs to be. You can simplify the test since if else means it will stop testing once one test is true, so just use >= and the lower value for all tests:

 let nota = prompt("Ingrese su nota: "); if (nota >= 90){ console.log(nota + " " + "equivale a una A"); } else if (nota >= 80) { console.log(nota + " " + "equivale a una B"); } else if (nota >= 70) { console.log(nota + " " + "equivale a una C"); } else if (nota >= 60) { console.log(nota + " " + "equivale a una D"); } else { console.log("Tienes una F"); } 

The tests have to be in the correct sequence, so if the value is 90 or greater, the first test is satisfied and no others are tested. If the value is say 82, the first test fails so it goes to the next test, which passes so the result is a B and again, no further tests are tried.

And so on for other values, so if the value is say 55, all the tests fail and it goes to the final else .

将表达式替换为nota >= 80 && nota <= 89等等。

use this code

  let nota = prompt("Ingrese su nota: "); if (nota >= 90){ console.log(nota + " " + "equivale a una A"); } else if (nota <= 89 && nota >= 80 ){ console.log(nota + " " + "equivale a una B"); } else if (nota <= 79 && nota >= 70){ console.log(nota + " " + "equivale a una C"); } else if (nota <= 69 && nota >= 60){ console.log(nota + " " + "equivale a una D"); } else{ console.log("Tienes una F"); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM