Skip to content

Instantly share code, notes, and snippets.

@daviddwlee84
Created August 3, 2023 11:02
Show Gist options
  • Save daviddwlee84/e1e223c5ff6bb6ef1694a11b69f52cb5 to your computer and use it in GitHub Desktop.
Save daviddwlee84/e1e223c5ff6bb6ef1694a11b69f52cb5 to your computer and use it in GitHub Desktop.
BMI Calculator
<!DOCTYPE html>
<html>
<head>
<title>BMI Calculator</title>
</head>
<body>
<h1>BMI Calculator</h1>
<label for="weight">Weight (kg):</label>
<input type="text" id="weight" />
<label for="height">Height (cm):</label>
<input type="text" id="height" />
<label for="bmi">BMI:</label>
<input type="text" id="bmi" />
<input type="button" value="Calculate" onclick="calculateBMI()" />
<script src="script.js"></script>
</body>
</html>
// https://chat.openai.com/share/991bdeb0-628a-4eed-b267-39f873f38af9
function calculateBMI() {
const weightInput = document.getElementById("weight");
const heightInput = document.getElementById("height");
const bmiInput = document.getElementById("bmi");
let weight = parseFloat(weightInput.value);
let height = parseFloat(heightInput.value) / 100; // Convert cm to meters
let bmi = parseFloat(bmiInput.value);
if (!isNaN(weight) && !isNaN(height)) {
bmi = (weight / (height * height)).toFixed(2);
bmiInput.value = bmi;
} else if (!isNaN(weight) && !isNaN(bmi)) {
height = (Math.sqrt(weight / bmi) * 100).toFixed(2);
heightInput.value = height;
} else if (!isNaN(bmi) && !isNaN(height)) {
weight = (bmi * (height * height)).toFixed(2);
weightInput.value = weight;
}
}
body {
font-family: Arial, sans-serif;
max-width: 400px;
margin: 0 auto;
padding: 20px;
}
h1 {
text-align: center;
}
label {
display: block;
margin-bottom: 5px;
}
input {
width: 100%;
padding: 5px;
margin-bottom: 10px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment