<html>
<head>
<title>Calculator</title>
<script type=text/javascript>
function calculator() {
while (operation != 0) {
var operation = prompt("What would you like to do?\n\n0. end the
program\n1. do addition\n2. do subtraction\n3. do multiplication\n4. do
division\n5. do square root\n", "Type you selection here");
if (operation == 1) {
var num1 = prompt("Enter the first number");
var num2 = prompt("Enter the second number");
alert ("The sum is " + (Number(num1)+Number(num2)));
} else if (operation == 2) {
var num1 = prompt("Enter the first number");
var num2 = prompt("Enter the second number");
alert ("The difference is " + (Number(num1)-Number(num2)));
} else if (operation == 3) {
var num1 = prompt("Enter the first number");
var num2 = prompt("Enter the second number");
alert ("The product is " + (Number(num1)*Number(num2)));
} else if (operation == 4) {
var num1 = prompt("Enter the first number");
var num2 = prompt("Enter the second number");
alert ("The quotiant is " + (Number(num1)/Number(num2)));
} else if (operation == 5) {
var num1 = prompt("Enter the number");
alert ("The square root is " + Math.sqrt(Number(num1)));
}
}
}
</script>
</head>
<body>
<h2><i>Repetition... Looping</i></h2>
<h1><font color=blue>Simple While Loop</font></h1>
<hr>
<center>
<input type=button value="run this program" onclick="calculator()">
</center>
<hr>
<h3>Description</h3>
<h3>Input</h3>
<h3>Output</h3>
<h3>Termination</h3>
</body>
</html>
http://forums.zybez.net/topic/1122395-creating-a-javascript-calculator-that-uses-prompts/
Post a Comment