-2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cálculo da média</title>
</head>
<body>
<h1>Cálculo da média</h1>
<input type="number" name="cal1" id="cal1">
<input type="number" name="cal2" id="cal2">
<input type="button" value="Calcular" onclick="calcular()">
<div id="res">
Testando
</div>
<script>
function calcular() {
//Nessa parte estou confuso, pois não sei o o que faço para converter
//Quando coloco os números no input number, ele da NaN
var num1 = window.document.getElementById('cal1')
var num2 = window.document.getElementById('cal2')
var calculo = (num1 + num2) / 2
var conversor = calculo
res.innerHTML = Number(conversor)
}
</script>
</body>
</html>
getElementById
returns the element, not its value. You need to take thevalue
and convert to number:var num1 = parseInt(document.getElementById('cal1').value)
(orparseFloat
if the number has decimal places)– hkotsubo