-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJavaScript.js
More file actions
138 lines (103 loc) · 4.23 KB
/
JavaScript.js
File metadata and controls
138 lines (103 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
let currentInput = document.getElementById("input")
let operandOne = 0
let operandTwo = 0
let operator = 0
let Num1 = document.getElementById('Num1');
let Num2 = document.getElementById('Num2');
let Num3 = document.getElementById('Num3');
let Num4 = document.getElementById('Num4');
let Num5 = document.getElementById('Num5');
let Num6 = document.getElementById('Num6');
let Num7 = document.getElementById('Num7');
let Num8 = document.getElementById('Num8');
let Num9 = document.getElementById('Num9');
let clear = document.getElementById('clear');
let divide = document.getElementById('divide');
let equals = document.getElementById('equals');
let plus = document.getElementById('plus');
let minus = document.getElementById('minus');
let times = document.getElementById('times');
let decimal = document.getElementById('decimal');
let calucation = document.getElementById('sum');
let symbols = ["+","-","*","/"];
function sum(op){
let calcArray = calucation.textContent.split("")
if((symbols.some(symbol => calcArray.indexOf(symbol) == calcArray.length -1))) {
calucation.textContent = (operandOne)
}
else { operandOne = currentInput.textContent;
calucation.textContent = operandOne;
currentInput.textContent = 0;}
if (op == "+") {
operator = 1;
calucation.textContent += "+"
}
else if (op == "-" && operandOne == 0) {
currentInput.textContent = "-"
}
else if (op == "-") {
operator = 2
calucation.textContent += "-"
}
else if (op == "*") {
operator = 3
calucation.textContent += "*"
}
else if (op == "/") {
operator = 4
calucation.textContent += "/"
}
if (calucation.textContent !== 0) {
calucation.style.color = 'black'
}
}
function answer(operator) {
operandTwo = currentInput.textContent
calucation.textContent += operandTwo
if (operator == 1) {
currentInput.textContent = Math.round( (parseFloat(operandOne) + parseFloat(operandTwo)) * 10000 ) / 10000
}
else if (operator == 2) {
currentInput.textContent = Math.round( (parseFloat(operandOne) - parseFloat(operandTwo)) * 10000) / 10000
}
else if (operator == 3) {
currentInput.textContent = Math.round( (parseFloat(operandOne) * parseFloat(operandTwo)) * 1000) /1000
}
else if (operator == 4) {
currentInput.textContent = Math.round( (parseFloat(operandOne) / parseFloat(operandTwo)) * 10000) /10000
}
operator = 0; operandOne = 0; operandTwo = 0;
if (calucation.textContent !== 0) {
calucation.style.color = 'black'
}
}
function numButton(num) {
if (currentInput.textContent != 0 || currentInput.textContent.includes("."))
{currentInput.textContent += num}
else if (currentInput.textContent == 0)
{currentInput.textContent = num}
}
Num1.addEventListener('click', () => { numButton(1)}, false);
Num2.addEventListener('click', () => { numButton(2)}, false);
Num3.addEventListener('click', () => { numButton(3)}, false);
Num4.addEventListener('click', () => { numButton(4)}, false);
Num5.addEventListener('click', () => { numButton(5)}, false);
Num6.addEventListener('click', () => { numButton(6)}, false);
Num7.addEventListener('click', () => { numButton(7)}, false);
Num8.addEventListener('click', () => { numButton(8)}, false);
Num9.addEventListener('click', () => { numButton(9)}, false);
Num0.addEventListener('click', () => { numButton(0)}, false);
plus.addEventListener('click', () => { sum('+')}, false);
minus.addEventListener('click', () => { sum("-")}, false);
times.addEventListener('click', () => { sum('*')}, false);
divide.addEventListener('click', () => { sum('/')}, false);
equals.addEventListener('click', () => { answer(operator)}, false);
decimal.addEventListener('click', function point() {
if (!currentInput.innerHTML.includes('.')) {
currentInput.textContent += '.'}}, false);
if (calucation.textContent == 0) {
calucation.style.color = 'white'
}
clear.addEventListener('click', function() {currentInput.textContent = 0;
calucation.textContent = 0; operandOne = 0; operandTwo = 0; operator = null; calucation.style.color = 'white'
}, false)