Está en la página 1de 1

<html>

<head>
<title>Sumar dos Numeros</title>
</head>
<body>
<form>
Número 1: <input id="num1" type="float" />
<br /><br />
Número 2: <input id="num2" type="float" />
<br /><br />
<input type="button" value="SUMAR" onclick="sumar();" /><br /
</form>
</body>

<script>
function sumar()
{
var num1 = document.getElementById('num1').value;
var num2 = document.getElementById('num2').value;

if (isNaN(parseFloat(document.getElementById('num1').value))) {
alert("Indique un número en 'numero1'");
document.getElementById("num1").innerText = "0";
document.getElementById("num1").focus();
} else if
(isNaN(parseFloat(document.getElementById('num2').value))) {
alert("Indique un número en 'numero2'");
document.getElementById("num2").innerText = "0";
document.getElementById("num2").focus();
}
else
{
var resultado = (parseFloat(num1) + parseFloat(num2));
alert("la suma es: " +resultado);
}
}
</script>
</html>

También podría gustarte