Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. var Calculator = {
  2. init: function() {
  3. this.output = document.getElementById('output');
  4. this.resultText = '';
  5. this.resultNumber = 0;
  6. },
  7.  
  8. onClick: function(e) {
  9. console.log(e.target.innerHTML);
  10. const buttonText = e.target.innerHTML;
  11. switch(buttonText) {
  12. case '+': this.operationPlus(buttonText); break;
  13. case '-': this.operationMinus(buttonText); break;
  14. case '*': this.operationMultiply(buttonText); break;
  15. case 'รท': this.operationDevide(buttonText); break;
  16. case '=': this.operationEval(); break;
  17. default: this.operationInputNumber(buttonText);
  18. }
  19. },
  20.  
  21. printText: function(text) {
  22. this.output.value = text;
  23. },
  24.  
  25. printResult: function() {
  26. this.printText(this.resultText);
  27. },
  28.  
  29. operationEval: function() {
  30. this.resultText = String(this.resultNumber);
  31. this.printResult();
  32. },
  33.  
  34. operationPlus: function(buttonText) {
  35. if (!this.isNumberInput) {
  36. this.resultNumber += this.lastNumber;
  37. }
  38.  
  39. this.lastNumber = Number(this.resultText);
  40. this.printText(buttonText);
  41. this.isNumberInput = false;
  42. },
  43.  
  44. operationMinus: function() {},
  45.  
  46. operationMultiply: function() {},
  47.  
  48. operationDevide: function() {},
  49.  
  50. operationInputNumber: function(buttonText) {
  51. if (!this.isNumberInput) {
  52. this.resultText = '';
  53. }
  54.  
  55. this.isNumberInput = true;
  56. this.resultText += buttonText;
  57. this.printResult();
  58. }
  59.  
  60. }n
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement