-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
115 lines (72 loc) · 2.39 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Project Calculator</title>
<link rel=" stylesheet" href="/Calculator/css/app.css">
</head>
<body>
<div class="section">
</div>
<script src="/Calculator/js/app.js"></script>
<!-- <script>
const btn1 = document.querySelector('.n1');
const btn2 = document.querySelector('.n2');
const btnAdd = document.querySelector('.opAdd');
const btnMinus = document.querySelector('.opMinus');
const btnDevide = document.querySelector('.opDevide');
const btnMultipy= document.querySelector('.opMultipy');
const total = document.querySelector('.total');
let result = 0;
let form = '';
let operator = '';
const arr = [];
let n1 = 0;
let n2 = 0;
function operate(item) {
if (typeof(item) === 'number') { //check for numbers only
form += item;
console.log(form);
} else { // if not number
console.log(item);// check the operator
arr.push(form);
arr.push(item);
form = '';
console.log(arr);
console.log('length: ' + arr.length);
if (item === '=') {
console.log('result: ' +arr[0]);
}
if (arr.length === 4) {//check if arr is equal to 4
switch (arr[1]) {
case '+':
result = +arr[0] + +arr[2];
break;
case '-':
result = +arr[0] - +arr[2];
break;
case '/':
result = +arr[0] / +arr[2];
break;
case '*':
result = +arr[0] * +arr[2];
break;
}
console.log(result); //show total
arr.splice(0, 3); // remove item 1-3
arr.unshift(result); // add result to the array
console.log(arr);
}
}
}
btn1.addEventListener('click', ()=> operate(5));
btn2.addEventListener('click', ()=> operate(3));
btnAdd.addEventListener('click', ()=> operate('+'));
btnMinus.addEventListener('click', ()=> operate('-'));
btnDevide.addEventListener('click', ()=> operate("/"));
btnMultipy.addEventListener('click', ()=> operate('*'));
total.addEventListener('click', ()=> operate('='));
</script> -->
</body>
</html>