-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
160 lines (144 loc) · 6.57 KB
/
index.html
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>BMI Calculator</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
class BMIApp extends React.Component {
render() {
return (
<div className="container">
<div className="row">
<div className="col-md-6 my-3">
<h1>BMI Calculator</h1>
<div className="form-group my-3">
<label>Weight</label>
<input onKeyPress={this.handleKeyPress} type="text" name="weight" id="weight" className="form-control" placeholder="" aria-describedby="helpId" autoFocus />
<small id="helpId" className="text-muted">kilograms</small>
</div>
<div className="form-group my-3">
<label>Height</label>
<input onKeyPress={this.handleKeyPress} type="text" name="height" id="height" className="form-control" placeholder="" aria-describedby="helpId" />
<small id="helpId" className="text-muted">centimetres</small>
</div>
<button onClick={this.calculateBMI} name="btnCalculate" id="btnCalculate" className="btn btn-primary" role="button">Calculate</button>
<button onClick={this.clearBMI} name="btnClear" id="btnClear" className="btn btn-secondary" role="button">Reset</button>
</div>
</div>
<BMIResult bmi={this.state.bmi} />
</div>
);
}
calculateBMI = () => {
let weight = document.getElementById("weight").value;
let height = document.getElementById("height").value;
let bmi = weight / (height / 100 * height / 100);
bmi = bmi.toFixed(1);
this.setState({
bmi: bmi,
weight: weight,
height: height
});
}
clearBMI = () => {
document.getElementById("weight").value = "";
document.getElementById("height").value = "";
this.setState({
bmi: 0,
weight: 0,
height: 0
});
}
handleKeyPress = (event) => {
if( event.key === "Enter" ) {
const weight = document.getElementById("weight").value;
const height = document.getElementById("height").value;
if( weight === "" || height === "" ) {
return;
}
this.calculateBMI();
}
}
constructor(props) {
super(props);
this.state = {
bmi: 0,
weight: 0,
height: 0
};
}
}
class BMIResult extends React.Component {
getBMICategory(bmi) {
let category = '';
let colourClass = '';
let textClass = '';
if( bmi < 16 ) {
category = 'Underweight (Severe thinness)';
colourClass = 'bg-danger';
textClass = 'text-white';
} else if( bmi >= 16 && bmi <= 16.9 ) {
category = 'Underweight (Moderate thinness)';
colourClass = 'bg-warning';
textClass = 'text-dark';
} else if( bmi >= 17 && bmi <= 18.4 ) {
category = 'Underweight (Mild thinness)';
colourClass = 'bg-warning';
textClass = 'text-dark';
} else if( bmi >= 18.5 && bmi <= 24.9 ) {
category = 'Normal range';
colourClass = 'bg-success';
textClass = 'text-white';
} else if( bmi >= 25 && bmi <= 29.9 ) {
category = 'Overweight (Pre-obese)';
colourClass = 'bg-warning';
textClass = 'text-dark';
} else if( bmi >= 30 && bmi <= 34.9 ) {
category = 'Obese (Class I)';
colourClass = 'bg-warning';
textClass = 'text-dark';
} else if( bmi >= 35 && bmi <= 39.9 ) {
category = 'Obese (Class II)';
colourClass = 'bg-danger';
textClass = 'text-white';
} else if( bmi >= 40 ) {
category = 'Obese (Class III)';
colourClass = 'bg-danger';
textClass = 'text-white';
}
return { category, colourClass, textClass };
}
render() {
const bmi = this.props.bmi > 0 ? this.props.bmi : "";
const { category, colourClass, textClass } = this.getBMICategory(bmi);
return (
<div className="row">
<div className="col-md-6 my-3">
{bmi && (
<div className={`card ${textClass} ${colourClass} mb-3`}>
<h5 className={`card-header ${textClass}`}>BMI Result</h5>
<div className="card-body">
<h5 className={`card-title ${textClass}`}>{bmi}</h5>
<p className={`card-text ${textClass}`}>{category}</p>
</div>
</div>
)}
</div>
</div>
);
}
}
const container = document.getElementById('root');
const root = ReactDOM.createRoot(container);
root.render(<BMIApp />);
</script>
</body>
</html>