forked from theonlyanson/Hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmi_calculator.js
43 lines (41 loc) · 1.07 KB
/
bmi_calculator.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
import React, { useState } from "react";
const App = () => {
const [bmi, setBmi] = useState();
const [info, setInfo] = useState();
const [height, setHeight] = useState();
const [weight, setWeight] = useState();
const handleBmi = () => {
let val = (
[Number(weight) / Number(height) / Number(height)] * 10000
).toFixed(1);
setBmi(val);
if (val < 18.5) {
setInfo("Under Weight");
} else if (val > 18.5 && val <= 24.9) {
setInfo("Healthy");
} else if (val > 24.9 && val < 30) {
setInfo("Overweight");
} else {
setInfo("Obese");
}
};
return (
<div>
<h1>BMI Calculator</h1>
<input
type="text"
onChange={(e) => setHeight(e.target.value)}
placeholder="height in cm"
/>
<input
type="text"
onChange={(e) => setWeight(e.target.value)}
placeholder="Weight in kg"
/>
<button onClick={handleBmi}>Calculate</button>
<h1>{bmi}</h1>
<h2>{info}</h2>
</div>
);
};
export default App;