Print a message from an input [closed]

-5

My goal is to make a code with javascript in which when entering a number in a field, it shows me a message with the student's grade. The code is as follows:

function calcular() {

  var 1 = 100;
  var 2 = 90;
  var 3 = 80;
  var 4 = 70;
  var 5 = 60;

  var nota = document.getElementById('nota').value;

  if (nota <
    var 1) {

    document.write("parametro invalido")
  }

  if (nota <
    var 2) {

    document.write("Examen Excelente")
  }

  if (nota <
    var 3) {

    document.write("Examen muy bueno")
  }

  if (nota <
    var 4) {

    document.write("Examen bueno")
  }

  if (nota <
    var 5) {

    document.write("Examen regular")
  }

  if (nota >
    var 5) {

    document.write("Desaprobado")
  }

}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>conversor</title>
</head>

<body>
  <input type="number" name="nota" id="nota">
  <button onclick="calcular()">calcular</button>
</body>

</html>
    
asked by condor12 25.07.2017 в 22:31
source

2 answers

2

What happens in the code?

1) Variables in both Javascript and most programming languages should not start with a number , this is what caused the error.

2) You must use the statement else if , so that messages are not repeated when the condition is fulfilled multiple times , when adding else if , only You can meet one of the conditions and therefore get the result you are looking for.

With this in mind, you could do something like this:

function calcular() {

  var uno = 100;
  var dos = 90;
  var tres = 80;
  var cuatro = 70;
  var cinco = 60;

  var nota = document.getElementById('nota').value;

  if (nota < uno) {
    document.write("parametro invalido")
  }

  else if (nota < dos) {
    document.write("Examen Excelente")
  }

  else if (nota < tres) {
    document.write("Examen muy bueno")
  }

  else if (nota < cuatro) {
    document.write("Examen bueno")
  }

  else if (nota < cinco) {
    document.write("Examen regular")
  }

  else if (nota > cinco) {
    document.write("Desaprobado")
  }

}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>conversor</title>
</head>

<body>
  <input type="number" id="nota" name="nota">
  <button onclick="calcular()">Calcular</button>
</body>

</html>
    
answered by 25.07.2017 / 22:43
source
0

The only mistake is how you name your variables, changing the names works well. Remember that the numbers are comparative, when you compare a variable numero that its value is 4 (for example) with a variable whose name is 1 can cause a compiled conflict since it is interpreted as a value.

function calcular() {

  var uno = 100;
  var dos = 90;
  var tres = 80;
  var cuatro = 70;
  var cinco = 60;

  var nota = document.getElementById('nota').value;

  if (nota < uno) {
    document.write("parametro invalido")
  } else if (nota < dos) {
    document.write("Examen Excelente")
  } else if (nota < tres) {
    document.write("Examen muy bueno")
  } else if (nota < cuatro) {
    document.write("Examen bueno")
  } else if (nota < cinco) {
    document.write("Examen regular")
  } else if (nota > cinco) {
    document.write("Desaprobado")
  }

}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>conversor</title>
</head>

<body>
  <input type="number" name="nota" id="nota">
  <button onclick="calcular()">calcular</button>
</body>

</html>
    
answered by 25.07.2017 в 22:44