-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_OLD.js
92 lines (77 loc) · 2.16 KB
/
script_OLD.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
let val1 = undefined;
let val2 = undefined;
let operator = undefined;
const digitContainer = document.querySelector('.digits');
const display = document.querySelector('.display');
const operatorContainer = document.querySelectorAll('.operations > button');
operatorContainer.forEach((button) => {
button.addEventListener('click', operate);
});
function createDigits(){
for (let i = 0; i < 10; i++) {
const digit = document.createElement('button');
digit.setAttribute('class', i);
digit.textContent = i;
digit.addEventListener('click', operate);
digitContainer.appendChild(digit);
}
}
function add(x, y){
return x + y;
}
function substract(x, y) {
return x - y;
}
function multiply(x, y) {
return x * y;
}
function divide(x, y) {
return x / y;
}
function calculate() {
switch (operator) {
case -1:
return display.textContent = add(val1, val2);
break;
case -2:
return display.textContent = substract(val1, val2);
break;
case -3:
return display.textContent = multiply(val1, val2);
break;
case -4:
return display.textContent = divide(val1, val2);
break;
default:
display.textContent = "Something broke lawl";
break;
}
}
function operate() {
value = parseInt(this.getAttribute('class'));
if(val1 === undefined && value >= 0){
console.log("Undefined!");
val1 = value;
display.textContent = value;
}
else if(val1 !== undefined && operator === undefined && value < 0){
console.log("operator!");
operator = value;
}
else if(val2 === undefined && val1 !== undefined && operator !== undefined && value >= 0){
val2 = value;
display.textContent = value;
}
else if(val2 !== undefined && val1 !== undefined && operator !== undefined && value < 0){
val1 = calculate();
if(value != -5){
operator = value;
}
else{
operator = undefined;
}
val2 = undefined;
}
console.log("Class: " + value);
}
createDigits();