forked from iBeiKeDevHomework/homework1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
61 lines (60 loc) · 1.3 KB
/
Copy pathindex.js
File metadata and controls
61 lines (60 loc) · 1.3 KB
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
// index.js
// 获取应用实例
const app = getApp()
Page({
data: {
num:'0',
op:''
},
result: null,
isClear: false,
numBtn: function(e) {
var num = e.target.dataset.val
if (this.data.num === '0' || this.isClear) {
this.setData({ num: num })
this.isClear = false
} else {
this.setData({ num: this.data.num + num })
}
},
opBtn: function(e) {
var op = this.data.op
var num = Number(this.data.num)
this.setData({ op: e.target.dataset.val })
if (this.isClear) {
return
}
this.isClear = true
if (this.result === null) {
this.result = num
return
}
if (op === '+'){
this.result = this.result + num
} else if (op === '-') {
this.result = this.result - num
} else if (op === '*') {
this.result = this.result * num
} else if (op === '/') {
this.result = this.result / num
} else if (op === '%') {
this.result = this.result % num
}
this.setData({ num: this.result + '' })
},
dotBtn: function(){
if(this.isClear){
this.isClear=false
return
}
this.setData({num:this.data.num+'.'})
},
delBtn:function(){
var num = this.data.num.substr(0,this.data.num.length-1)
this.setData({num: num==='' ?'0' :num})
},
resetBtn:function(){
this.result = null
this.isClear = false
this.setData({ num:'0',op:''})
}})