Basic calculator, dynamic operator
by lasha
HTML
<input id="item1" type="text">
<select id="selectOperator">
<option value="add">add</option>
<option value="subtract">subtract</option>
<option value="divide">divide</option>
</select>
<input id="item2" type="text"><br>
<button id="submitBtn">RESULT</button><br><br>
Result of math: <div class="math-result"></div>
JavaScript
(function(){
var submitBtn = document.getElementById("submitBtn");
var resultBox = document.querySelector(".math-result");
var mathOperator;
console.log(submitBtn, resultBox);
var getKeys = function(obj){
var keys = [];
for(var key in obj){
keys.push(key);
}
return keys;
}
function doTheMath(operator, value1, value2){
var mathResult;
value1 = parseInt(value1);
value2 = parseInt(value2);
switch(operator) {
case 'add':
console.log("add");
mathResult = value1 + value2;
break;
case 'subtract':
console.log("sub");
mathResult = value1 - value2;
break;
case 'divide':
console.log("divide");
mathResult = value1 / value2;
break;
default:
alert("please select operator");
}
return mathResult;
}
submitBtn.onclick = function(e){
var value1 = document.getElementById("item1").value;
var value2 = document.getElementById("item2").value;
mathOperator = document.getElementById("selectOperator").value;
resultBox.innerHTML = doTheMath(mathOperator, value1, value2);
};
})();