-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
69 lines (65 loc) · 1.61 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
let output = document.getElementsByTagName('p')[0];
let equal = document.getElementById('equal');
let fnum=''
let sign = true
let result = 0
let rnum
function concat(num) {
// rnum = parseInt(num)
fnum = fnum + num
output.innerText = fnum;
}
function expression(oper,final) {
if (oper.includes('+')) {
result = parseInt(final[0]) + result + parseInt(final[1]);
}else if (oper.includes('-')) {
result = parseInt(final[0]) - parseInt(final[1]) - result;
plusMin()
}else if (oper.includes('x')){
if (!result) {
result = parseInt(final[0]) * parseInt(final[1]) * 1;
}else{
result = parseInt(final[0]) * parseInt(final[1]) * result;
}
}else if (oper.includes('/')) {
if (!result) {
result = (parseInt(final[0]) / parseInt(final[1])) / 1;
}else{
result = (parseInt(final[0]) / parseInt(final[1])) / result;
}
}
// fnum = fnum + oper
// output.innerText = fnum;
// operator = oper
}
function empty(){
fnum=''
output.innerText = '0';
result=0
}
function plusMin(){
if (sign) {
output.innerText = '-' + fnum;
sign = false
}else{
output.innerText = fnum;
sign = true
}
}
function percent() {
fnum = fnum / 100;
output.innerText = fnum;
}
equal.addEventListener('click',()=>{
let final = fnum.split(/[+-/x]/)
expression(fnum,final)
fnum = result
result=0
output.innerText = fnum;
console.log(fnum)
})
function clr() {
let len = fnum.length;
fnum = fnum.slice(0,len-1)
output.innerText = fnum;
}