//when the button is clicked
document.getElementById("submit").addEventListener("click",calculateIt);
function calculateIt() {
//create some variables
var myOperator;
var returnValue;
//get the operands
var operand1 = document.getElementById("operand1").value;
var operand2 = document.getElementById("operand2").value;
//get the operand types
var select1 = document.getElementById("operand1-type");
var select2 = document.getElementById("operand2-type");
var operand1type = select1.value;
var operand2type = select2.value;
//get the operator
var radios = document.getElementsByName('operator');
//convert the operands
switch (operand1type) {
case "string":
operand1 = String(operand1);
break;
case "number":
operand1 = Number(operand1);
break;
}
switch (operand2type) {
case "string":
operand2 = String(operand2);
break;
case "number":
operand2 = Number(operand2);
break;
}
//loop through each possible operand value and find the checked one
for (var i = 0, length = radios.length; i < length; i++) {
if (radios[i].checked) {
myOperator = radios[i].value;
//do a different operation depending on which operator was selected
switch (radios[i].value) {
case "+":
returnValue = operand1 + operand2;
break;
case "-":
returnValue = operand1 - operand2;
break;
case "*":
returnValue = operand1 * operand2;
break;
case "/":
returnValue = operand1 * operand2;
break;
case "%":
returnValue = operand1 % operand2;
break;
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.