Skip to content

Commit a9de63b

Browse files
committed
Almost finish
1 parent b85d910 commit a9de63b

File tree

1 file changed

+39
-8
lines changed

1 file changed

+39
-8
lines changed

App.js

+39-8
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,40 @@ class App extends React.Component {
66
constructor() {
77
super()
88
this.state = {
9-
calculationText: ""
9+
calculationText: "",
10+
resultText: ''
1011
}
12+
this.operation = ['Del', '+', '-', '*', '/'];
1113
}
1214

1315
calculateResult() {
1416
const text = this.state.calculationText
17+
console.log(text, eval(text))
18+
this.setState({
19+
resultText: eval(text)
20+
})
21+
//BODMAS
22+
// eval(text)
1523
// now parse this text
1624
}
1725

26+
validate() {
27+
const text = this.state.calculationText
28+
switch(text.slice(-1)) {
29+
case '+':
30+
case '-':
31+
case '*':
32+
case '/':
33+
return false
34+
}
35+
return true
36+
}
37+
1838
buttonbPressed(text) {
1939
//console.log(text)
2040

2141
if(text == '=') {
22-
return this.calculateResult()
42+
return this.validate() && this.calculateResult()
2343
}
2444
this.setState({
2545
calculationText: this.state.calculationText + text
@@ -31,10 +51,22 @@ class App extends React.Component {
3151
case 'Del' :
3252
let text = this.state.calculationText.split('')
3353
text.pop()
34-
3554
this.setState({
3655
calculationText: text.join('')
3756
})
57+
break;
58+
case '+':
59+
case '-':
60+
case '*':
61+
case '/':
62+
const lastChar = this.state.calculationText.split('').pop()
63+
64+
if(this.operation.indexOf(lastChar) > 0) return
65+
66+
if(this.state.text == "") return
67+
this.setState({
68+
calculationText: this.state.calculationText + operation
69+
})
3870
}
3971
}
4072

@@ -54,11 +86,10 @@ class App extends React.Component {
5486
}
5587

5688
let oper = [];
57-
let operation = ['Del', '+', '-', '*', '/'];
58-
for(let i = 0; i < 4; i++) {
89+
for(let i = 0; i < 5; i++) {
5990
oper.push(
60-
<TouchableOpacity style={styles.btn} onPress={() => this.operate(operation[i])}>
61-
<Text style={[styles.btnText, {color: 'white'}]}>{operation[i]}</Text>
91+
<TouchableOpacity style={styles.btn} onPress={() => this.operate(this.operation[i])}>
92+
<Text style={[styles.btnText, {color: 'white'}]}>{this.operation[i]}</Text>
6293
</TouchableOpacity>
6394
)
6495
}
@@ -68,7 +99,7 @@ class App extends React.Component {
6899
<Text style={styles.calculationText}>{this.state.calculationText}</Text>
69100
</View>
70101
<View style={styles.result}>
71-
<Text style={styles.resultText}>121</Text>
102+
<Text style={styles.resultText}>{this.state.resultText}</Text>
72103
</View>
73104
<View style={styles.buttons}>
74105
<View style={styles.numbers}>

0 commit comments

Comments
 (0)