/
javascript.js
149 lines (129 loc) · 4.04 KB
/
javascript.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
function add() {
let sum = a + b;
console.log(sum);
document.querySelector("#inputfield").value = sum;
}
function subtract() {
let product = a - b;
console.log(product);
document.querySelector("#inputfield").value = product;
}
function multiply() {
let multiplier = a * b;
console.log(multiplier);
document.querySelector("#inputfield").value = multiplier;
}
function divide() {
let divider = a / b;
if (b === 0) {
divider = "oh naw boss";
}
console.log(divider);
document.querySelector("#inputfield").value = divider;
}
function operate(a, b) {
if (operator == "+") {
add(a, b);
} else if (operator == "-") {
subtract(a, b);
} else if (operator == "*") {
multiply(a, b);
} else if (operator == "/") {
divide(a, b);
}
}
document.querySelector(".add").addEventListener("click", function () {
a = parseFloat(document.querySelector("#inputfield").value);
operator = "+";
document.querySelector("#inputfield").value = "";
console.log(operator);
console.log(a);
});
document.querySelector(".subtract").addEventListener("click", function () {
a = parseFloat(document.querySelector("#inputfield").value);
document.querySelector("#inputfield").value = "";
operator = "-";
console.log(operator);
});
document.querySelector(".multiply").addEventListener("click", function () {
a = parseFloat(document.querySelector("#inputfield").value);
operator = "*";
document.querySelector("#inputfield").value = "";
console.log(operator);
});
document.querySelector(".divide").addEventListener("click", function () {
a = parseFloat(document.querySelector("#inputfield").value);
operator = "/";
document.querySelector("#inputfield").value = "";
console.log(operator);
});
document.querySelector("#equals").addEventListener("click", function () {
b = parseFloat(document.querySelector("#inputfield").value);
operate(a, b) = result;
console.log(result);
});
const clear = document.querySelector(".clear");
clear.addEventListener("click", function () {
document.querySelector("#inputfield").value = "";
document.querySelector(".decimal").disabled = false;
console.clear();
a = null;
b = null;
operator = null;
});
const num1 = document.querySelector(".num1");
num1.addEventListener("click", function () {
const num1 = 1;
document.querySelector("#inputfield").value += num1;
});
const num2 = document.querySelector(".num2");
num2.addEventListener("click", function () {
const num2 = 2;
document.querySelector("#inputfield").value += num2;
});
const num3 = document.querySelector(".num3");
num3.addEventListener("click", function () {
const num3 = 3;
document.querySelector("#inputfield").value += num3;
});
const num4 = document.querySelector(".num4");
num4.addEventListener("click", function () {
const num4 = 4;
document.querySelector("#inputfield").value += num4;
});
const num5 = document.querySelector(".num5");
num5.addEventListener("click", function () {
const num5 = 5;
document.querySelector("#inputfield").value += num5;
});
const num6 = document.querySelector(".num6");
num6.addEventListener("click", function () {
const num6 = 6;
document.querySelector("#inputfield").value += num6;
});
const num7 = document.querySelector(".num7");
num7.addEventListener("click", function () {
const num7 = 7;
document.querySelector("#inputfield").value += num7;
});
const num8 = document.querySelector(".num8");
num8.addEventListener("click", function () {
const num8 = 8;
document.querySelector("#inputfield").value += num8;
});
const num9 = document.querySelector(".num9");
num9.addEventListener("click", function () {
const num9 = 9;
document.querySelector("#inputfield").value += num9;
});
const num0 = document.querySelector(".num0");
num0.addEventListener("click", function () {
const num0 = 0;
document.querySelector("#inputfield").value += num0;
});
const decimal = document.querySelector(".decimal");
decimal.addEventListener("click", function () {
const decimal = ".";
document.querySelector("#inputfield").value += decimal;
document.querySelector(".decimal").disabled = true;
});