-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathApp.js
146 lines (138 loc) · 5.29 KB
/
App.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import React from 'react';
import {StyleSheet, Text, View, TouchableOpacity, Vibration} from 'react-native';
import {useState} from 'react';
import { Entypo } from '@expo/vector-icons';
export default function App() {
const [darkMode, setDarkMode] = useState(false);
const [currentNumber, setCurrentNumber] = useState('');
const [lastNumber, setLastNumber] = useState('');
const buttons = ['C', 'DEL', '/', 7, 8, 9, '*', 4, 5, 6, '-', 1, 2, 3, '+', 0, '.', '=']
function calculator() {
let lastArr = currentNumber[currentNumber.length-1];
if(lastArr === '/' || lastArr === '*' || lastArr === '-' || lastArr === '+' || lastArr === '.') {
setCurrentNumber(currentNumber)
return
}
else {
let result = eval(currentNumber).toString();
setCurrentNumber(result)
return
}
}
function handleInput(buttonPressed) {
if(buttonPressed === '+' || buttonPressed === '-' || buttonPressed === '*' || buttonPressed === '/') {
Vibration.vibrate(35);
setCurrentNumber(currentNumber + buttonPressed)
return
}
else if (buttonPressed === 1 || buttonPressed === 2 || buttonPressed === 3 || buttonPressed === 4 || buttonPressed === 5 ||
buttonPressed === 6 || buttonPressed === 7 || buttonPressed === 8 || buttonPressed === 9 || buttonPressed === 0 || buttonPressed === '.' ) {
Vibration.vibrate(35);
}
switch(buttonPressed) {
case 'DEL':
Vibration.vibrate(35);
setCurrentNumber(currentNumber.substring(0, (currentNumber.length - 1)))
return
case 'C':
Vibration.vibrate(35);
setLastNumber('')
setCurrentNumber('')
return
case '=':
Vibration.vibrate(35);
setLastNumber(currentNumber + '=')
calculator()
return
}
setCurrentNumber(currentNumber + buttonPressed)
}
const styles = StyleSheet.create({
results: {
backgroundColor: darkMode ? '#282f3b' : '#f5f5f5',
maxWidth: '100%',
minHeight: '35%',
alignItems: 'flex-end',
justifyContent: 'flex-end',
},
resultText: {
maxHeight: 45,
color: '#00b9d6',
margin: 15,
fontSize: 35,
},
historyText: {
color: darkMode ? '#B5B7BB' : '#7c7c7c',
fontSize: 20,
marginRight: 10,
alignSelf: 'flex-end',
},
themeButton: {
alignSelf: 'flex-start',
bottom: '5%',
margin: 15,
backgroundColor: darkMode ? '#7b8084' : '#e5e5e5',
alignItems: 'center',
justifyContent: 'center',
width: 50,
height: 50,
borderRadius: 25,
},
buttons: {
width: '100%',
height: '35%',
flexDirection: 'row',
flexWrap: 'wrap',
},
button: {
borderColor: darkMode ? '#3f4d5b' : '#e5e5e5',
alignItems: 'center',
justifyContent: 'center',
minWidth: '24%',
minHeight: '54%',
flex: 2,
},
textButton: {
color: darkMode ? '#b5b7bb' : '#7c7c7c',
fontSize: 28,
}
})
return(
<View>
<View style={styles.results}>
<TouchableOpacity style={styles.themeButton}>
<Entypo name={darkMode ? 'light-up' : 'moon'} size={24} color={darkMode ? 'white' : 'black'} onPress={() => darkMode ? setDarkMode(false) : setDarkMode(true)}/>
</TouchableOpacity>
<Text style={styles.historyText}>{lastNumber}</Text>
<Text style={styles.resultText}>{currentNumber}</Text>
</View>
<View style={styles.buttons}>
{buttons.map((button) =>
button === '=' || button === '/' || button === '*' || button === '-' || button === '+' ?
<TouchableOpacity key={button} style={[styles.button, {backgroundColor: '#00b9d6'} ]} onPress={() => handleInput(button)}>
<Text style={[styles.textButton, {color: 'white', fontSize: 28} ]}>{button}</Text>
</TouchableOpacity>
:
button === 0 ?
<TouchableOpacity key={button} style={[styles.button, {backgroundColor: typeof(button) === 'number' ? darkMode ? '#303946' : '#fff' : darkMode === true ? '#414853' : '#ededed', minWidth: '36%'} ]} onPress={() => handleInput(button)}>
<Text style={styles.textButton}>{button}</Text>
</TouchableOpacity>
:
button === '.' || button === 'DEL' ?
<TouchableOpacity key={button} style={[styles.button, {backgroundColor: button === '.' ? darkMode ? '#303946' : '#fff' : darkMode === true ? '#414853' : '#ededed', minWidth: '37%'} ]} onPress={() => handleInput(button)}>
<Text style={styles.textButton}>{button}</Text>
</TouchableOpacity>
:
button === 'C' ?
<TouchableOpacity key={button} style={[styles.button, {backgroundColor: typeof(button) === 'number' ? darkMode ? '#303946' : '#fff' : darkMode === true ? '#414853' : '#ededed', minWidth: '36%'} ]} onPress={() => handleInput(button)}>
<Text style={styles.textButton}>{button}</Text>
</TouchableOpacity>
:
<TouchableOpacity key={button} style={[styles.button, {backgroundColor: typeof(button) === 'number' ? darkMode ? '#303946' : '#fff' : darkMode === true ? '#414853' : '#ededed' } ]} onPress={() => handleInput(button)}>
<Text style={styles.textButton}>{button}</Text>
</TouchableOpacity>
)}
</View>
</View>
);
}