Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement