| 1 | class Calculator { |
| 2 | constructor(calcElement, resultElement){ |
| 3 | this.calcElement = calcElement; |
| 4 | this.resultElement = resultElement; |
| 5 | this.clear(); |
| 6 | } |
| 7 | |
| 8 | clear(){ |
| 9 | this.currentOperant = ''; |
| 10 | this.previousOperant = ''; |
| 11 | this.operation = undefined; |
| 12 | } |
| 13 | |
| 14 | delete(){ |
| 15 | this.currentOperant = this.currentOperant.toString().slice(0, -1); |
| 16 | } |
| 17 | |
| 18 | appendNumber(number){ |
| 19 | if(number === '.' && this.currentOperant.includes('.')) |
| 20 | return; |
| 21 | this.currentOperant = this.currentOperant.toString() + number.toString(); |
| 22 | } |
| 23 | |
| 24 | chooseOperation(operation){ |
| 25 | if(this.operation === '') |
| 26 | return; |
| 27 | if(this.previousOperant !== ''){ |
| 28 | this.compute(); |
| 29 | } |
| 30 | this.operation = operation; |
| 31 | this.previousOperant = this.currentOperant; |
| 32 | this.currentOperant = ''; |
| 33 | } |
| 34 | |
| 35 | compute(){ |
| 36 | let computation; |
| 37 | let prev = parseFloat(this.previousOperant); |
| 38 | let current = parseFloat(this.currentOperant); |
| 39 | |
| 40 | if(isNaN(prev) || isNaN(current)) |
| 41 | return; |
| 42 | switch(this.operation){ |
| 43 | case '+' : |
| 44 | computation = prev + current; |
| 45 | break; |
| 46 | case '−' : |
| 47 | computation = prev - current; |
| 48 | break; |
| 49 | case '×' : |
| 50 | computation = prev * current; |
| 51 | break; |
| 52 | case '÷' : |
| 53 | computation = prev / current; |
| 54 | break; |
| 55 | default: |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | this.currentOperant = computation; |
| 60 | this.operation = undefined; |
nothing calls this directly
no outgoing calls
no test coverage detected