How To Calculate 2 Numbers Using Javascript
In This Javascript Tutorial we will See How To Calculate 2 Numbers ( Sum, Mutiplication, Division, Substraction ) Using Input Text, Button And Select Option In JS Using Netbeans Editor .
Project Source Code:
<!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>
9 comments
commentsCan you help me with the html/javascript code of (n*90+200)*1.02 when user will give the value of n?
Replyfunction calcTest(n) {
Replyreturn((n*90+200)*1.02);
}
var n= 5;
document.write(calcTest(n));
Return num after multiplying it by ten.
ReplyPlease I was asked to write a program that accepts 2 values and performs both addition, multiplication, subtraction,division and highest value among the two values..I need help
ReplyTq sir
ReplyHvala ti stari reso si mi lajf pri vrboti
ReplyNot working
ReplyWe are enter the strings it shows error to set the validation
Replyhow to change dropdown into buttons
Reply