<!DOCTYPE html>
<html>
<head>
<title>javascript: calculate two numbers</title>
<meta charset="windows-1252">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
function calc()
{
var n1 = parseFloat(document.getElementById('n1').value);
var n2 = parseFloat(document.getElementById('n2').value);
var oper = document.getElementById('operators').value;
if(oper === '+')
{
document.getElementById('result').value = n1+n2;
}
if(oper === '-')
{
document.getElementById('result').value = n1-n2;
}
if(oper === '/')
{
document.getElementById('result').value = n1/n2;
}
if(oper === 'X')
{
document.getElementById('result').value = n1*n2;
}
}
</script>
</head>
<body>
<input type="text" id="n1"/><br/><br/>
<input type="text" id="n2"/><br/><br/>
<select id="operators">
<option value="+">+</option>
<option value="-">-</option>
<option value="/">/</option>
<option value="X">X</option>
</select>
<button onclick="calc();">=</button>
<input type="text" id="result"/>
</body>
</html>
0 Comments