-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
180 lines (168 loc) · 5.89 KB
/
script.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// CREATING ELEMENT FOR TITLE
const header = document.createElement("h1");
const para = document.createElement("p");
// Calculator Divisons Here
const container = document.createElement("div");
const input = document.createElement("input");
const firstdiv = document.createElement("div");
const seconddiv = document.createElement("div");
const thirddiv = document.createElement("div");
const fourthdiv = document.createElement("div");
const fivethdiv = document.createElement("div");
// 1st Row
const clear = document.createElement("button");
const dot = document.createElement("button");
const backspace = document.createElement("button");
const multiply = document.createElement("button");
// 2nds Row
const seven = document.createElement("button");
const eight = document.createElement("button");
const nine = document.createElement("button");
const division = document.createElement("button");
// 3rd Row
const four = document.createElement("button");
const five = document.createElement("button");
const six = document.createElement("button");
const subtract = document.createElement("button");
// 4th Row
const one = document.createElement("button");
const two = document.createElement("button");
const three = document.createElement("button");
const addition = document.createElement("button");
// 5th Row
const zero = document.createElement("button");
const doublezero = document.createElement("button");
const equal = document.createElement("button");
//-------------- ADDING ATTRIBUTES---------------------------
header.setAttribute("id", "title");
para.setAttribute("id", "description");
container.setAttribute("id", "container");
input.setAttribute("id", "result");
input.setAttribute("type", "text");
// 1st Row
firstdiv.setAttribute("id", "firstdiv");
clear.className = "first";
clear.setAttribute("id", "clear");
clear.addEventListener("click", clearall);
dot.className = "first";
dot.setAttribute("id", "dot");
dot.setAttribute("onclick", "display('.')");
backspace.className = "first";
backspace.setAttribute("id", "backspace");
backspace.addEventListener("click", deleted);
multiply.className = "first";
multiply.setAttribute("id", "multiply");
multiply.setAttribute("onclick", "display('*')");
// 2nd ROw
seconddiv.setAttribute("id", "seconddiv");
seven.setAttribute("class", "second");
seven.setAttribute("onclick", "display('7')");
eight.className = "second";
eight.setAttribute("onclick", "display('8')");
nine.className = "second";
nine.setAttribute("onclick", "display('9')");
division.className = "second";
division.setAttribute("id", "division");
division.setAttribute("onclick", "display('/')");
// 3rd Row
thirddiv.setAttribute("id", "thirddiv");
four.className = "third";
four.setAttribute("onclick", "display('4')");
five.className = "third";
five.setAttribute("onclick", "display('5')");
six.className = "third";
six.setAttribute("onclick", "display('6')");
subtract.className = "third";
subtract.setAttribute("id", "subtract");
subtract.setAttribute("onclick", "display('-')");
// 4t Row
fourthdiv.setAttribute("id", "fourthdiv");
one.className = "fourth";
one.setAttribute("id", "1");
one.setAttribute("onclick", "display('1')");
two.className = "fourth";
two.setAttribute("id", "two");
two.setAttribute("onclick", "display('2')");
three.className = "fourth";
three.setAttribute("id", "three");
three.setAttribute("onclick", "display('3')");
addition.className = "fourth";
addition.setAttribute("id", "add");
addition.setAttribute("onclick", "display('+')");
// 5th Row
fivethdiv.setAttribute("id", "fivethdiv");
zero.className = "fiveth";
zero.setAttribute("onclick", "display('0')");
doublezero.className = "fiveth";
doublezero.setAttribute("onclick", "display('00')");
equal.className = "fiveth";
equal.setAttribute("id", "equal");
equal.setAttribute("onclick", "total()");
//--------------- Inner Text for Elements---------------------
header.innerText = "ONLINE CALCULATOR";
para.innerText = " Using JS DOM ";
// 1st Row for InnerText
clear.innerText = "C";
dot.innerText = "•";
backspace.innerText = "←";
multiply.innerText = "x";
// 2nd Row for InnerText
seven.innerText = "7";
eight.innerText = "8";
nine.innerText = "9";
division.innerText = "/";
// 3rd Row for InnerText
four.innerText = "4";
five.innerText = "5";
six.innerText = "6";
subtract.innerText = "-";
// 4th Row for InnerText
one.innerText = "1";
two.innerText = "2";
three.innerText = "3";
addition.innerText = "+";
// 5th Row for InnerText
zero.innerText = "0";
doublezero.innerText = "00";
equal.innerText = "=";
//------------- Append Child to Parent Div------------------------
firstdiv.append(clear, dot, backspace, multiply);
seconddiv.append(seven, eight, nine, division);
thirddiv.append(four, five, six, subtract);
fourthdiv.append(one, two, three, addition);
fivethdiv.append(zero, doublezero, equal);
container.append(input, firstdiv, seconddiv, thirddiv, fourthdiv, fivethdiv);
// Append Elements to Body
document.body.append(header, para, container);
//----------------------------FUNCTIONS-----------------------------------
input.addEventListener("input", () => {
if (
input.value != "0" ||
input.value != "1" ||
input.value != "2" ||
input.value != "3" ||
input.value != "4" ||
input.value != "5" ||
input.value != "6" ||
input.value != "7" ||
input.value != "8" ||
input.value != "9"
) {
alert("Words Are Not Allowed Here !");
}
});
function display(val) {
var i = (document.querySelector("#result").value += val);
}
function clearall() {
document.querySelector("#result").value = "";
}
function deleted() {
var dele = document.querySelector("#result");
dele.value = dele.value.substring(0, dele.value.length - 1);
}
function total() {
let x = document.getElementById("result").value;
let y = eval(x);
document.getElementById("result").value = y;
}