-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.jsx
56 lines (50 loc) · 1.78 KB
/
App.jsx
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
import { useState } from 'react';
import './App.css';
function Calculator() {
const [input, setInput] = useState("");
const [result, setResult] = useState("");
const handleClick = (value) => {
setInput(input + value);
};
const calculate = () => {
try {
setResult(eval(input)); // Caution: eval can be a security risk; consider alternatives for production.
} catch (error) {
console.error(error);
setResult("Error");
}
};
const clear = () => {
setInput("");
setResult("");
};
return (
<div className="calculator">
<h1>React Calculator</h1>
<div className="display">
<input type="text" value={input} readOnly />
<h2>{result}</h2>
</div>
<div className="buttons">
<button onClick={() => handleClick("1")}>1</button>
<button onClick={() => handleClick("2")}>2</button>
<button onClick={() => handleClick("3")}>3</button>
<button onClick={() => handleClick("+")}>+</button>
<button onClick={() => handleClick("4")}>4</button>
<button onClick={() => handleClick("5")}>5</button>
<button onClick={() => handleClick("6")}>6</button>
<button onClick={() => handleClick("-")}>-</button>
<button onClick={() => handleClick("7")}>7</button>
<button onClick={() => handleClick("8")}>8</button>
<button onClick={() => handleClick("9")}>9</button>
<button onClick={() => handleClick("*")}>*</button>
<button onClick={() => handleClick("0")}>0</button>
<button onClick={() => handleClick(".")}>.</button>
<button onClick={calculate}>=</button>
<button onClick={() => handleClick("/")}>/</button>
<button onClick={clear}>C</button>
</div>
</div>
);
}
export default Calculator;