Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AminBusiness/8b1752840c40abf59bbe558454d71b97 to your computer and use it in GitHub Desktop.
Save AminBusiness/8b1752840c40abf59bbe558454d71b97 to your computer and use it in GitHub Desktop.
JS Bin// source https://jsbin.com/layoxap
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
//Calculator application
var num1 = 0;
var num2 = 0;
var op = 0;
var ans = 0.0;
var theOp = "";
var again = true;
var wanna = "";
var first = "";
function main()
{
while(again) // Equivalent to while(again == true)
{
num1 = inputANumber("first")
num2 = inputANumber("second")
op = chooseOperation();
generateAnswer();
theOp = generateTheOp();
alert("First = " + num1 + "\nSecond = " + num2 +
"\nOperation = " + op + "\nAnswer = " + ans +
"\ntheOp = " + theOp);
printResult();
wanna = prompt("Do you wish to run the program again? ", "Y");
first = wanna.charAt(0).toUpperCase();
if(first !== "Y")
{
again = false;
}
}
}
function inputANumber(order)
{
var n = parseInt(prompt("Please enter the " + order + "number:", "100"));
while(isNaN(n))
{
alert ("You must enter a numeric value")
n = parseFloat(prompt("Please enter the " + order + "number:", "100"));
}
return n;
}
function chooseOperation()
{
var str = "Please enter a 1 to add the 2 numbers\n";
str += " Please enter a 2 to subrtact num2 fron num1\n";
str += "Please enter a 3 to multiply the 2 numbers\n";
str += "Please enter a 4 to divide num1 by num2\n\n";
str += "Please enter a 1, 2, 3, or 4 now: ";
return parseInt(prompt(str, "1"));
}
//Call right routine to generate answer
function generateAnswer()
{
switch(op)
{
case 1:
ans = add();
break;
case 2:
ans = subtract();
break;
case 3:
ans = multiply();
break;
case 4:
ans = divide();
break;
default:
chooseOperation();
break;
}
}
function add()
{
return num1 + num2;
}
function subtract()
{
return num1 - num2;
}
function multiply()
{
return num1 * num2;
}
function divide()
{
//Do not allow illegal divide by 0
if(num2 === 0)
{
return "Illegal attempt to divide by 0"
}
else
{
return (num1 / num2).fixed(2);
}
}
function generateTheOp()
{
var theOp = "";
switch(op)
{
case 1:
theOp = "+";
break;
case 1:
theOp = "-";
break;
case 1:
theOp = "*";
break;
case 1:
theOp = "/";
break;
default:
chooseOperation();
}
}
function printResult()
{
alert (num1 + " " + theOp + " " + num2 + " = " + ans)
}
</script>
<script id="jsbin-source-javascript" type="text/javascript">//Calculator application
var num1 = 0;
var num2 = 0;
var op = 0;
var ans = 0.0;
var theOp = "";
var again = true;
var wanna = "";
var first = "";
function main()
{
while(again) // Equivalent to while(again == true)
{
num1 = inputANumber("first")
num2 = inputANumber("second")
op = chooseOperation();
generateAnswer();
theOp = generateTheOp();
alert("First = " + num1 + "\nSecond = " + num2 +
"\nOperation = " + op + "\nAnswer = " + ans +
"\ntheOp = " + theOp);
printResult();
wanna = prompt("Do you wish to run the program again? ", "Y");
first = wanna.charAt(0).toUpperCase();
if(first !== "Y")
{
again = false;
}
}
}
function inputANumber(order)
{
var n = parseInt(prompt("Please enter the " + order + "number:", "100"));
while(isNaN(n))
{
alert ("You must enter a numeric value")
n = parseFloat(prompt("Please enter the " + order + "number:", "100"));
}
return n;
}
function chooseOperation()
{
var str = "Please enter a 1 to add the 2 numbers\n";
str += " Please enter a 2 to subrtact num2 fron num1\n";
str += "Please enter a 3 to multiply the 2 numbers\n";
str += "Please enter a 4 to divide num1 by num2\n\n";
str += "Please enter a 1, 2, 3, or 4 now: ";
return parseInt(prompt(str, "1"));
}
//Call right routine to generate answer
function generateAnswer()
{
switch(op)
{
case 1:
ans = add();
break;
case 2:
ans = subtract();
break;
case 3:
ans = multiply();
break;
case 4:
ans = divide();
break;
default:
chooseOperation();
break;
}
}
function add()
{
return num1 + num2;
}
function subtract()
{
return num1 - num2;
}
function multiply()
{
return num1 * num2;
}
function divide()
{
//Do not allow illegal divide by 0
if(num2 === 0)
{
return "Illegal attempt to divide by 0"
}
else
{
return (num1 / num2).fixed(2);
}
}
function generateTheOp()
{
var theOp = "";
switch(op)
{
case 1:
theOp = "+";
break;
case 1:
theOp = "-";
break;
case 1:
theOp = "*";
break;
case 1:
theOp = "/";
break;
default:
chooseOperation();
}
}
function printResult()
{
alert (num1 + " " + theOp + " " + num2 + " = " + ans)
}</script></body>
</html>
//Calculator application
var num1 = 0;
var num2 = 0;
var op = 0;
var ans = 0.0;
var theOp = "";
var again = true;
var wanna = "";
var first = "";
function main()
{
while(again) // Equivalent to while(again == true)
{
num1 = inputANumber("first")
num2 = inputANumber("second")
op = chooseOperation();
generateAnswer();
theOp = generateTheOp();
alert("First = " + num1 + "\nSecond = " + num2 +
"\nOperation = " + op + "\nAnswer = " + ans +
"\ntheOp = " + theOp);
printResult();
wanna = prompt("Do you wish to run the program again? ", "Y");
first = wanna.charAt(0).toUpperCase();
if(first !== "Y")
{
again = false;
}
}
}
function inputANumber(order)
{
var n = parseInt(prompt("Please enter the " + order + "number:", "100"));
while(isNaN(n))
{
alert ("You must enter a numeric value")
n = parseFloat(prompt("Please enter the " + order + "number:", "100"));
}
return n;
}
function chooseOperation()
{
var str = "Please enter a 1 to add the 2 numbers\n";
str += " Please enter a 2 to subrtact num2 fron num1\n";
str += "Please enter a 3 to multiply the 2 numbers\n";
str += "Please enter a 4 to divide num1 by num2\n\n";
str += "Please enter a 1, 2, 3, or 4 now: ";
return parseInt(prompt(str, "1"));
}
//Call right routine to generate answer
function generateAnswer()
{
switch(op)
{
case 1:
ans = add();
break;
case 2:
ans = subtract();
break;
case 3:
ans = multiply();
break;
case 4:
ans = divide();
break;
default:
chooseOperation();
break;
}
}
function add()
{
return num1 + num2;
}
function subtract()
{
return num1 - num2;
}
function multiply()
{
return num1 * num2;
}
function divide()
{
//Do not allow illegal divide by 0
if(num2 === 0)
{
return "Illegal attempt to divide by 0"
}
else
{
return (num1 / num2).fixed(2);
}
}
function generateTheOp()
{
var theOp = "";
switch(op)
{
case 1:
theOp = "+";
break;
case 1:
theOp = "-";
break;
case 1:
theOp = "*";
break;
case 1:
theOp = "/";
break;
default:
chooseOperation();
}
}
function printResult()
{
alert (num1 + " " + theOp + " " + num2 + " = " + ans)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment