Skip to content

Instantly share code, notes, and snippets.

@AmilKey
Created January 17, 2017 17:01
Show Gist options
  • Save AmilKey/d9eaf9f5c35615edef131519825a8af1 to your computer and use it in GitHub Desktop.
Save AmilKey/d9eaf9f5c35615edef131519825a8af1 to your computer and use it in GitHub Desktop.
calc
var Calculator = {
init: function() {
this.output = document.getElementById('output');
this.resultText = '';
this.resultNumber = 0;
},
onClick: function(e) {
console.log(e.target.innerHTML);
const buttonText = e.target.innerHTML;
switch(buttonText) {
case '+': this.operationPlus(buttonText); break;
case '-': this.operationMinus(buttonText); break;
case '*': this.operationMultiply(buttonText); break;
case '÷': this.operationDevide(buttonText); break;
case '=': this.operationEval(); break;
default: this.operationInputNumber(buttonText);
}
},
printText: function(text) {
this.output.value = text;
},
printResult: function() {
this.printText(this.resultText);
},
operationEval: function() {
this.resultText = String(this.resultNumber);
this.printResult();
},
operationPlus: function(buttonText) {
if (!this.isNumberInput) {
this.resultNumber += this.lastNumber;
}
this.lastNumber = Number(this.resultText);
this.printText(buttonText);
this.isNumberInput = false;
},
operationMinus: function() {},
operationMultiply: function() {},
operationDevide: function() {},
operationInputNumber: function(buttonText) {
if (!this.isNumberInput) {
this.resultText = '';
}
this.isNumberInput = true;
this.resultText += buttonText;
this.printResult();
}
}n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment