Javascript media calculation by HTML form

Asked

Viewed 2,291 times

0

I need the average to be shown 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>

1 answer

0


Try it this way:

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’ve helped.

  • It did, thank you

  • Friend if you can score the answer.

Browser other questions tagged

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