BMI Calculator
Info
Created On: September 15, 2023
Created By:
Tags
AI
Model: gpt-3.5-turbo-16k-0613
Time: 97 seconds
Prompt Tokens: 1127
Completion Tokens: 1820
Total Token Cost: 2947
Get This App On Your Website
Copy Code
1. Copy the code above with the iframe and link.
2. Paste the code into your website.
3. Resize the iframe to fit your website.
Javascript, HTML, CSS Code
Copy
<html>
<head>
<script type="text/javascript"> window.addEventListener('error', function(event) { var message = JSON.parse(JSON.stringify(event.message)); var source = event.filename; var lineno = event.lineno; var colno = event.colno; var error = event.error; window.parent.postMessage({ type: 'iframeError', details: { message: message, source: source, lineno: lineno, colno: colno, error: error ? error.stack : '' } }, '*'); }); window.addEventListener('unhandledrejection', function(event) { window.parent.postMessage({ type: 'iframePromiseRejection', details: { reason: event.reason } }, '*'); }); </script>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>BMI Calculator</title>
<meta name="description" content="Calculate your BMI using this simple tool">
<meta name="keywords" content="BMI, Body Mass Index, Health, Weight, Height">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<script type="text/javascript">
try {
// App Javascript Goes Here. Place your entire script content inside the try block for error handling.
// This will run when the DOM is ready.
document.addEventListener("DOMContentLoaded", function() {
// Initialize user interface elements
const unitOfMeasurement = $("#unit-of-measurement");
const heightInput = $("#height-input");
const weightInput = $("#weight-input");
const calculateButton = $("#calculate-button");
const resultContainer = $("#result-container");
// Set default unit of measurement to METRIC
let selectedUnitOfMeasurement = "METRIC";
// Event listener for unit of measurement selection
unitOfMeasurement.on("change", function() {
selectedUnitOfMeasurement = unitOfMeasurement.val();
// Toggle visibility of height input based on selected unit of measurement
if (selectedUnitOfMeasurement === "METRIC") {
$("#height-metric").show();
$("#height-imperial").hide();
} else {
$("#height-metric").hide();
$("#height-imperial").show();
}
});
// Event listener for calculate button click
calculateButton.on("click", function() {
const height = selectedUnitOfMeasurement === "METRIC" ? parseFloat(heightInput.val()) : calculateImperialHeight();
const weight = parseFloat(weightInput.val());
// Input validation
if (isNaN(height) || isNaN(weight)) {
alert("Please enter valid height and weight values.");
return;
}
// Validate height and weight ranges
if (selectedUnitOfMeasurement === "METRIC") {
if (height < 140 || height > 210 || weight < 40 || weight > 200) {
alert("Please enter height between 140 cm and 210 cm, and weight between 40 kg and 200 kg.");
return;
}
} else {
if (height < 55 || height > 83 || weight < 88 || weight > 440) {
alert("Please enter height between 4'7\" and 6'11\", and weight between 88 lbs and 440 lbs.");
return;
}
}
// Calculate BMI
const bmi = selectedUnitOfMeasurement === "METRIC" ? calculateMetricBMI(height, weight) : calculateImperialBMI(height, weight);
// Display results
displayResults(bmi);
});
// Function to calculate BMI for METRIC unit
function calculateMetricBMI(height, weight) {
const heightInMeters = height / 100;
return weight / (heightInMeters ** 2);
}
// Function to calculate BMI for IMPERIAL unit
function calculateImperialBMI(height, weight) {
const heightInInches = calculateImperialHeight();
return (weight / (heightInInches ** 2)) * 703;
}
// Function to calculate height in inches for IMPERIAL unit
function calculateImperialHeight() {
const feet = parseFloat($("#feet-input").val());
const inches = parseFloat($("#inches-input").val());
return (feet * 12) + inches;
}
// Function to display BMI results
function displayResults(bmi) {
let interpretation;
if (bmi < 18.5) {
interpretation = "Underweight";
} else if (bmi >= 18.5 && bmi < 25) {
interpretation = "Normal weight";
} else if (bmi >= 25 && bmi < 30) {
interpretation = "Overweight";
} else {
interpretation = "Obese";
}
resultContainer.html(`Your BMI is: <strong>${bmi.toFixed(2)}</strong><br>Interpretation: <strong>${interpretation}</strong>`);
}
});
} catch (error) {
// This will throw the error to the parent window.
throw error;
}
</script>
<style>
body {
font-family: 'Roboto', sans-serif;
background-color: #f5f5f5;
}
h1 {
color: #333333;
margin-top: 20px;
margin-bottom: 30px;
font-size: 28px;
font-weight: 500;
text-align: center;
}
.container {
max-width: 500px;
margin: 0 auto;
padding: 30px;
background-color: #ffffff;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.form-group {
margin-bottom: 20px;
}
.form-label {
font-weight: 500;
}
.form-input {
width: 100%;
padding: 10px;
border-radius: 5px;
border: 1px solid #cccccc;
}
.button-container {
display: flex;
justify-content: center;
margin-top: 30px;
}
.button {
padding: 10px 20px;
border-radius: 5px;
background-color: #007bff;
color: #ffffff;
font-size: 16px;
font-weight: 500;
cursor: pointer;
}
.button:hover {
background-color: #0056b3;
}
#result-container {
margin-top: 30px;
text-align: center;
}
</style>
<link rel="canonical" href="https://calculator.tools/prompt/6119/">
<meta charset="utf-8">
</head>
<body>
<div id="main-container" class="container">
<h1>BMI Calculator</h1>
<div class="form-group">
<label class="form-label">Unit of Measurement:</label>
<select id="unit-of-measurement" class="form-input">
<option value="METRIC">Metric</option>
<option value="IMPERIAL">Imperial</option>
</select>
</div>
<div id="height-metric" class="form-group">
<label class="form-label">Height (cm):</label>
<input id="height-input" class="form-input" type="number" min="140" max="210" step="1">
</div>
<div id="height-imperial" class="form-group" style="display: none;">
<label class="form-label">Height (ft):</label>
<input id="feet-input" class="form-input" type="number" min="4" max="6" step="1">
<label class="form-label">Height (in):</label>
<input id="inches-input" class="form-input" type="number" min="0" max="11" step="1">
</div>
<div class="form-group">
<label class="form-label">Weight:</label>
<input id="weight-input" class="form-input" type="number" min="40" max="200" step="1">
</div>
<div class="button-container">
<button id="calculate-button" class="button">Calculate</button>
</div>
<div id="result-container"></div>
</div>
<script type="text/javascript"> var localStoragePrefix = "ct-{{ cachebreaker }}"; var lastSave = 0; function saveLocal(data) { if (Date.now() - lastSave < 1000) { return; } let cookie = localStoragePrefix + "=" + JSON.stringify(data) + "; path=" + window.location.pathname + "'; SameSite=Strict"; cookie += "; expires=" + new Date(Date.now() + 1000 * 60 * 60 * 24 * 365 * 1000).toUTCString(); document.cookie = cookie; lastSave = Date.now(); } function loadLocal() { var cookiePrefix = localStoragePrefix + "="; var cookieStart = document.cookie.indexOf(cookiePrefix); if (cookieStart > -1) { let cookieEnd = document.cookie.indexOf(";", cookieStart); if (cookieEnd == -1) { cookieEnd = document.cookie.length; } var cookieData = document.cookie.substring(cookieStart + cookiePrefix.length, cookieEnd); return JSON.parse(cookieData); } } </script>
<script type="text/javascript"> window.addEventListener('load', function() { var observer = new MutationObserver(function() { window.parent.postMessage({height: document.documentElement.scrollHeight || document.body.scrollHeight},"*"); }); observer.observe(document.body, {attributes: true, childList: true, subtree: true}); window.parent.postMessage({height: document.documentElement.scrollHeight || document.body.scrollHeight},"*"); }); </script>
</body>
</html>