|
1 | 1 | let lastOperand = 0;
|
2 | 2 | let operation = null;
|
3 | 3 |
|
| 4 | +const display = document.getElementById('display'); |
4 | 5 | const inputWindow = document.getElementById('inputWindow');
|
5 | 6 |
|
6 |
| - |
7 | 7 | document.getElementById('btn_clr').addEventListener('click', function () {
|
8 | 8 | lastOperand = 0;
|
9 | 9 | operation = null;
|
10 |
| - inputWindow.value = ''; |
11 |
| -}) |
| 10 | + inputWindow.value = 0; |
| 11 | +}); |
| 12 | + |
| 13 | +function number(n){return document.getElementById('btn_'+n).addEventListener('click', function () { |
| 14 | + inputWindow.value +=n; |
| 15 | + inputWindow.value = parseInt(inputWindow.value, 10)}) |
| 16 | +}; |
| 17 | + |
| 18 | +number (0); |
| 19 | +number (1); |
| 20 | +number (2); |
| 21 | +number (3); |
| 22 | +number (4); |
| 23 | +number (5); |
| 24 | +number (6); |
| 25 | +number (7); |
| 26 | +number (8); |
| 27 | +number (9); |
| 28 | + |
| 29 | +document.getElementById('btn_plus').addEventListener('click', function () { |
| 30 | + lastOperand = parseFloat(inputWindow.value); |
| 31 | + operation = "plus"; |
| 32 | + inputWindow.value = 0; |
| 33 | +}); |
| 34 | +document.getElementById('btn_minus').addEventListener('click', function () { |
| 35 | + lastOperand = parseFloat(inputWindow.value); |
| 36 | + operation = "minus"; |
| 37 | + inputWindow.value = 0; |
| 38 | +}); |
| 39 | +document.getElementById('btn_times').addEventListener('click', function () { |
| 40 | + lastOperand = parseFloat(inputWindow.value); |
| 41 | + operation = "times"; |
| 42 | + inputWindow.value = 0; |
| 43 | +}); |
| 44 | +document.getElementById('btn_diveded').addEventListener('click', function () { |
| 45 | + lastOperand = parseFloat(inputWindow.value); |
| 46 | + operation = "diveded"; |
| 47 | + inputWindow.value = 0; |
| 48 | +}); |
| 49 | +document.getElementById('btn_root').addEventListener('click', function () { |
| 50 | + lastOperand = parseFloat(inputWindow.value); |
| 51 | + operation = "root"; |
| 52 | + inputWindow.value = "sqrt("+ lastOperand+")"; |
| 53 | + }); |
| 54 | +document.getElementById('btn_realNumber').addEventListener('click', function () { |
| 55 | + lastOperand = parseFloat(inputWindow.value); |
| 56 | + operation = "realNumber"; |
| 57 | + inputWindow.value = lastOperand/10; |
| 58 | + }); |
| 59 | +document.getElementById('btn_minusNumber').addEventListener('click', function () { |
| 60 | + lastOperand = parseFloat(inputWindow.value); |
| 61 | + operation = "minusNumber"; |
| 62 | + inputWindow.value = -lastOperand; |
| 63 | + }); |
| 64 | + |
| 65 | +document.getElementById('btn_calc').addEventListener('click', function () { |
| 66 | + if (operation === "plus"){ |
| 67 | + const result1= lastOperand + "+" + parseFloat(inputWindow.value); |
| 68 | + const result= lastOperand + parseFloat(inputWindow.value); |
| 69 | + operation - null; |
| 70 | + lastOperand = 0; |
| 71 | + inputWindow.value = result; |
| 72 | + display.value = result1+ "=" + result; |
| 73 | + } |
| 74 | + if (operation === "minus"){ |
| 75 | + const result1= lastOperand + "-" + parseFloat(inputWindow.value); |
| 76 | + const result= lastOperand - parseFloat(inputWindow.value); |
| 77 | + operation - null; |
| 78 | + lastOperand = 0; |
| 79 | + inputWindow.value = result; |
| 80 | + display.value = result1+ "=" + result; |
| 81 | + } |
| 82 | + if (operation === "times"){ |
| 83 | + const result1= lastOperand + "*" + parseFloat(inputWindow.value); |
| 84 | + const result= lastOperand * parseFloat(inputWindow.value); |
| 85 | + operation - null; |
| 86 | + lastOperand = 0; |
| 87 | + inputWindow.value = result; |
| 88 | + display.value = result1+ "=" + result; } |
| 89 | + if (operation === "diveded"){ |
| 90 | + const result1= lastOperand + "/" + parseFloat(inputWindow.value); |
| 91 | + const result= lastOperand / parseFloat(inputWindow.value); |
| 92 | + operation - null; |
| 93 | + lastOperand = 0; |
| 94 | + inputWindow.value = result; |
| 95 | + display.value = result1+ "=" + result; |
| 96 | + } |
| 97 | + if (operation === "root"){ |
| 98 | + const result1="sqrt("+ lastOperand+")"; |
| 99 | + const result= Math.sqrt(lastOperand); |
| 100 | + operation - null; |
| 101 | + lastOperand = 0; |
| 102 | + inputWindow.value = result; |
| 103 | + display.value = result1+ "=" + result; |
| 104 | + } |
| 105 | +}); |
| 106 | + |
| 107 | + |
12 | 108 |
|
0 commit comments