-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
129 lines (116 loc) · 3.5 KB
/
main.js
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
let startingNum = document.querySelector('#display');
let secondDisplay = document.querySelector('#second-display');
let newNum;
let num1;
let operator = '';
let total = 0;
let digits = document.querySelectorAll(".all-buttons button");
digits.forEach(button => {
button.onclick = function() {
if (newNum === undefined && button.innerText !== undefined) {
newNum = button.innerText;
} else {
//Prevents multiple decimal points
if (button.innerText === '.' && newNum.includes('.')) {
return;
}
newNum += button.innerText
}
startingNum.innerText = newNum
};
});
let backspace = document.querySelector("#backspace");
backspace.onclick = function() {
let currentText = startingNum.textContent;
let updatedText = currentText.slice(0, -1);
startingNum.textContent = updatedText;
newNum = updatedText;
// if (operator != '') {
// newNum = undefined
// }
console.log(startingNum.textContent, newNum)
}
function add() {
return Number(num1) + Number(newNum)
}
function subtract() {
return Number(num1) - Number(newNum)
}
function multiply() {
return Number(num1) * Number(newNum)
}
function divide() {
return Number(num1) / Number(newNum)
}
function modulo() {
return Number(num1) % Number(newNum)
}
function clear() {
startingNum.textContent = '0';
secondDisplay.textContent = ''
newNum = undefined;
operator = '';
num1 = '0';
total = '0';
}
let clearScrn = document.querySelector('#clear');
clearScrn.onclick = clear
function operate() {
if (num1 === undefined) {
num1 = '0'
} else if (newNum === undefined){
total = num1;
startingNum.textContent = total;
} else if ( operator == '÷' && newNum == '0'){
startingNum.textContent = "Stop that";
clear;
} else if ( operator == '%' && newNum == '0'){
startingNum.textContent = "Stop that";
clear;
} else if (operator == '+') {
total = add();
startingNum.textContent = total;
console.log(num1,operator,newNum + " = " + total);
} else if (operator == '-') {
total = subtract();
startingNum.textContent = total;
console.log(num1,operator,newNum + " = " + total);
} else if (operator == '÷') {
total = divide();
startingNum.textContent = total;
console.log(num1,operator,newNum + " = " + total);
} else if (operator == 'x') {
total = multiply();
startingNum.textContent = total;
console.log(num1,operator,newNum + " = " + total);
} else if (operator == '%') {
total = modulo();
startingNum.textContent = total;
console.log(num1,operator,newNum + " = " + total);
}
if (operator != '') {
secondDisplay.textContent = num1 + ' ' + operator + ' ' + newNum + " = ";
}
num1 = String(total);
}
let equals = document.querySelector('#equals')
equals.onclick = operate;
let operators = document.querySelectorAll("#operators");
operators.forEach(button => {
button.onclick = function() {
if (operator == '') {
num1 = newNum;
} else if (operator != '') {
if (num1 == undefined) {
num1 = '0'
}
if (newNum == undefined) {
newNum = '0'
}
operate();
secondDisplay.textContent = num1 + ' ' + operator;
}
operator = button.innerText;
newNum = undefined;
}
});