|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +var Calculator = function (container) { |
| 4 | + this.container = container; |
| 5 | + this.init(); |
| 6 | +}; |
| 7 | + |
| 8 | +Calculator.prototype = { |
| 9 | + init: function () { |
| 10 | + document.myCalculators = document.myCalculators || []; |
| 11 | + document.myCalculators.push(this); |
| 12 | + this.createLayout(); |
| 13 | + this.str = 0; |
| 14 | + this.updateDisplay(); |
| 15 | + }, |
| 16 | + |
| 17 | + input: function (input) { |
| 18 | + if (this.str) |
| 19 | + this.str += input; |
| 20 | + else |
| 21 | + this.str = input; |
| 22 | + |
| 23 | + this.updateDisplay(); |
| 24 | + }, |
| 25 | + |
| 26 | + output: function (input) { |
| 27 | + return this.validate() || eval(this.str); |
| 28 | + }, |
| 29 | + |
| 30 | + validate: function () { |
| 31 | + var str = this.str; |
| 32 | + var reg = /[^1-9,0,*,/,\-,+,%, ,(,)]/g; |
| 33 | + var result = str.match(reg); |
| 34 | + // return ; |
| 35 | + if (result || str.endsWith("+") || str.endsWith("-") || str.endsWith("*") || str.endsWith("/")) { |
| 36 | + return "Invalid: " + (result || "End of character."); |
| 37 | + } |
| 38 | + |
| 39 | + }, |
| 40 | + |
| 41 | + updateDisplay: function (result) { |
| 42 | + document.getElementById("c-header").innerHTML = result || this.str; |
| 43 | + }, |
| 44 | + |
| 45 | + sum() { |
| 46 | + this.updateDisplay(this.output()); |
| 47 | + }, |
| 48 | + |
| 49 | + clear: function () { |
| 50 | + this.str = 0; |
| 51 | + this.updateDisplay(); |
| 52 | + // console.log(this.output()); |
| 53 | + }, |
| 54 | + |
| 55 | + createLayout: function () { |
| 56 | + var cont = document.getElementById(this.container); |
| 57 | + cont.appendChild(this.createHeader()); |
| 58 | + cont.appendChild(this.createBody()); |
| 59 | + }, |
| 60 | + |
| 61 | + createHeader: function () { |
| 62 | + var cHeader = document.createElement("div"); |
| 63 | + cHeader.innerHTML = `<div id="c-header"> |
| 64 | + <span id="c-display">Display</span> |
| 65 | + </div>`; |
| 66 | + return cHeader; |
| 67 | + |
| 68 | + }, |
| 69 | + |
| 70 | + //no function |
| 71 | + createButton(text, i) { |
| 72 | + var button = document.createElement("button"); |
| 73 | + button.innerHTML = text; |
| 74 | + var buttonObserver = this.buttonObserver; |
| 75 | + button.addEventListener("click", function () { buttonObserver(this.innerHTML) }); |
| 76 | + return button; |
| 77 | + |
| 78 | + }, |
| 79 | + |
| 80 | + buttonObserver(a) { |
| 81 | + var currentCalc = document.myCalculators[0]; |
| 82 | + if (a == "C") { |
| 83 | + currentCalc.clear(); |
| 84 | + } |
| 85 | + else if (a == "=") { |
| 86 | + currentCalc.sum(); |
| 87 | + } |
| 88 | + else { |
| 89 | + currentCalc.input(a); |
| 90 | + } |
| 91 | + }, |
| 92 | + |
| 93 | + createBody() { |
| 94 | + var cBody = document.createElement("div"); |
| 95 | + for (var i = 0; i < 10; i++) { |
| 96 | + cBody.appendChild(this.createButton(i)); |
| 97 | + } |
| 98 | + |
| 99 | + cBody.appendChild(this.createButton("+")); |
| 100 | + cBody.appendChild(this.createButton("-")); |
| 101 | + cBody.appendChild(this.createButton("*")); |
| 102 | + cBody.appendChild(this.createButton("/")); |
| 103 | + cBody.appendChild(this.createButton("=")); |
| 104 | + cBody.appendChild(this.createButton("(")); |
| 105 | + cBody.appendChild(this.createButton(")")); |
| 106 | + cBody.appendChild(this.createButton("C")); |
| 107 | + |
| 108 | + cBody.style.background = "green"; |
| 109 | + return cBody; |
| 110 | + } |
| 111 | + |
| 112 | +}; |
| 113 | + |
| 114 | +var a = new Calculator("myCalculator"); |
| 115 | +//sum("51 + (5 / 2 * 85 + 1)");//Invalid: End of character. |
| 116 | +//sum("5 / 2 * 85 + 1asd"); // Invalid: a,s,d |
| 117 | +//sum("5 + (5 / 2 * 85 + 1)") // 218.5 |
| 118 | + |
| 119 | + |
| 120 | +//a.clear(); |
| 121 | + |
| 122 | +function sum(string) { a.input(string) } |
| 123 | + |
| 124 | + |
0 commit comments