Nothing is Something Ideas in JS
Taking the inspiration from this awesome Tech Talk and trying to see ways in which this philosophy can be applied to javascript.
by Marcus Baptiste
HTML
<input type='text' id='numa' />
<input type='text' id='oper' />
<input type='text' id='numb' />
<input type='button' id='calc' value='Calculate'/>
CSS
input[type=text] {
width: 40px;
margin-right: 10px;
}
input[type=button] {
display: block;
margin-top: 10px;
}
JavaScript
var txtNumA = document.getElementById('numa');
var txtNumB = document.getElementById('numb');
var txtOper = document.getElementById('oper');
var btnCalc = document.getElementById('calc');
var opcOper = {
"+": function(a, b) {
return a + b;
},
"-": function(a, b) {
return a - b;
},
"/": function(a, b) {
return a / b;
},
"*": function(a, b) {
return a * b;
}
};
btnCalc.onclick = function() {
try {
var numA = parseInt(txtNumA.value);
var numB = parseInt(txtNumB.value);
var oper = txtOper.value;
alert(opcOper[oper](numA, numB));
} catch(e) {
console.log('Input error: ' + e);
return false;
}
};