Repository navigation
Expand file tree
/
Copy pathApp.js
More file actions
141 lines (133 loc) · 3.58 KB
/
Copy pathApp.js
File metadata and controls
141 lines (133 loc) · 3.58 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
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
import { StatusBar } from 'expo-status-bar';
import { useState } from 'react';
import { SafeAreaView, StyleSheet, Text, TextInput, Touchable, TouchableOpacity, View } from 'react-native';
import { Colors } from 'react-native/Libraries/NewAppScreen';
import Icon from 'react-native-vector-icons/MaterialIcons';
export default function App() {
const [input, setInput] = useState('')
const [result, setResult] = useState('')
const onButtonPress = (value) => {
if (value === '=') {
try {
setResult(eval(input));
} catch (error) {
console.log(error);
setResult('error')
}
} else if (value === 'C') {
setInput('');
setResult('')
} else if (value === 'DEL') {
setInput(input.slice(0, -1));
} else {
setInput(input + value)
}
}
// Function to add thousand separators
const formatNumberWithThousandSeparators = (num) => {
if (num === 'error') {
return 'error';
}
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
};
return (
<SafeAreaView style={styles.container}>
<StatusBar style="auto" />
<View style={styles.resultsContainer}>
<Text style={styles.resultText}>
{formatNumberWithThousandSeparators(result)}
</Text>
</View>
<View style={styles.inputContainer}>
<TextInput
thousandSeparator={true}
style={styles.textInput}
value={formatNumberWithThousandSeparators(input)}
onChangeText={setInput}
keyboardType={'numeric'}
/>
<TouchableOpacity
style={styles.deletebutton}
onPress={() => onButtonPress('DEL')}
>
<Icon name="backspace" style={styles.deletebuttonIcon} />
</TouchableOpacity>
</View>
<View
style={styles.lineStyle}
/>
<View style={styles.buttonContainer}>
{["7", "8", "9", "4", "5", "6", "1", "2", "3", "0", "-", "+", '/', "*", "=", "C",].map(
(item, index) => (
<TouchableOpacity
key={index}
style={styles.button}
onPress={() => onButtonPress(item)}
>
<Text style={styles.buttontext}>{item}</Text>
</TouchableOpacity>
)
)}
</View>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
}, resultsContainer: {
flex: 2,
justifyContent: 'center',
alignItems: 'flex-end'
}, resultText: {
fontSize: 40,
alignItems: 'center',
color: '#024C8E',
}, inputContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'flex-end'
}, textInput: {
flex: 7,
padding: 2,
fontSize: 30,
alignItems: 'flex-start',
width: '100%',
}, buttonContainer: {
flex: 7,
flexWrap: 'wrap',
flexDirection: 'row',
padding: 5,
}, button: {
backgroundColor: "#ECE5E3",
padding: 25,
margin: 4,
width: '30%',
alignItems: 'center',
borderWidth: 1,
borderBottomLeftRadius: 100,
borderBottomRightRadius: 100,
borderTopLeftRadius: 100,
borderTopRightRadius: 100,
borderColor: '#ccc',
}, buttontext: {
fontSize: 25,
fontWeight: "bold",
textAlign: "center",
color: "#04187E",
}, lineStyle: {
borderBottomColor: 'grey',
borderBottomWidth: StyleSheet.hairlineWidth,
}, deletebutton: {
padding: "30",
fontWeight: "bold",
textAlign: "center",
color: "#04187E",
}, deletebuttonIcon: {
fontSize: 30,
borderBottomColor: '#04187E',
fontWeight: "bold",
textAlign: "center",
color: "#780101",
}
});