Average calculation in JavaScript using HTML form [duplicate]

0

I need the average to be displayed by an alert ()

HTML:

function calculoMedia(n1, n2, n3, n4) {
  var media;
  var n1 = getElementById('n1').value;
  var n2 = getElementById('n2').value;
  var n3 = getElementById('n3').value;
  var n4 = getElementById('n4').value;
  media = (n1 + n2 + n3 + n4) / 4;
  alert("Média das notas: " + media + "");
}
<!DOCTYPE HTML>
<html>

<head>
  <meta charset="UTF-8" />
  <script language="JavaScript" src="code.js"></script>
  <title>Médias</title>
</head>

<body>
  <form>
    Nota 1:<br>
    <input type="number" id="n1"><br> Nota 2:<br>
    <input type="number" id="n2"><br> Nota 3:<br>
    <input type="number" id="n3"><br> Nota 4:<br>
    <input type="number" id="n4"><br><br>
    <input type="button" onclick="javascript:calculoMedia();" value="Calcular Média">
  </form>
</body>

</html>
    
asked by anonymous 14.06.2018 / 18:59

1 answer

0

Try this:

function calculoMedia()
{
  var media;
  var n1 = parseInt(document.getElementById('n1').value);
  var n2 = parseInt(document.getElementById('n2').value);
  var n3 = parseInt(document.getElementById('n3').value);
  var n4 = parseInt(document.getElementById('n4').value);
  media = (n1 + n2 + n3 + n4) / 4;
  alert("Média das notas: " + media + "");

}

I hope I have helped.

    
14.06.2018 / 19:06