-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculater.js
74 lines (63 loc) · 2.39 KB
/
calculater.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
var myClear = document.querySelector('.clear');
var myValue = document.querySelector('.val');
var myBtn = document.getElementsByClassName('input'); // [ ]
var shekelToDolar = document.querySelector('.shekelToDolar');
var dolarToShekel = document.querySelector('.dolarToShekel');
var shekelToEuro = document.querySelector('.shekelToEuro');
var euroToShekel = document.querySelector('.euroToShekel');
myClear.onclick = () => {
myValue.value = '';
};
var arrayOfOp = ['-', '+', '/', 'x', '='];
var opCunt = 0;
for (i = 0; i < myBtn.length; i++) {
myBtn[i].onclick = function () {
if (arrayOfOp.includes(this.value)) {
opCunt++;
if (opCunt > 1) {
var thisOp = '';
for (i = 0; i < arrayOfOp.length; i++) {
if (myValue.value.indexOf(arrayOfOp[i]) !== -1) {
thisOp = arrayOfOp[i];
break;
}
}
var arrayOfNam = myValue.value.split(thisOp); // ["",""]
var resultForTowNum = 0;
if (thisOp === '+') {
resultForTowNum = parseFloat(arrayOfNam[0]) + parseFloat(arrayOfNam[1]);
} else if (thisOp === '-') {
resultForTowNum = parseFloat(arrayOfNam[0]) - parseFloat(arrayOfNam[1]);
} else if (thisOp === 'x') {
resultForTowNum = parseFloat(arrayOfNam[0]) * parseFloat(arrayOfNam[1]);
} else if (thisOp === '/') {
resultForTowNum = parseFloat(arrayOfNam[0]) / parseFloat(arrayOfNam[1]);
}
if (this.value === '=') {
opCunt--;
}
myValue.value = resultForTowNum;
opCunt--;
}
}
if (this.value !== '=') {
// result without equal
myValue.value += this.value;
}
};
}
del.onclick = function () {
myValue.value = myValue.value.substring(0, myValue.value.length - 1);
};
shekelToDolar.onclick = function () {
myValue.value = parseFloat(myValue.value) / 3.3;
};
dolarToShekel.onclick = function() {
myValue.value = parseFloat(myValue.value) * 3.3;
};
shekelToEuro.onclick = function () {
myValue.value = parseFloat(myValue.value) / 3.94;
};
euroToShekel.onclick = function () {
myValue.value = parseFloat(myValue.value) * 3.94;
};