Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created July 1, 2023 01:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/4282a8fd83aadfa7251b065fd5d76ecb to your computer and use it in GitHub Desktop.
Save code-boxx/4282a8fd83aadfa7251b065fd5d76ecb to your computer and use it in GitHub Desktop.
Javascript BMI Calculator

JAVASCRIPT BMI CALCULATOR

https://code-boxx.com/simple-javascript-bmi-calculator/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

/* (X) DOES NOT MATTER, COSMETICS */
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
#bmi-form input[type=text], #bmi-form input[type=number] {
border: 1px solid #cfcfcf;
padding: 5px;
}
#bmi-form input[type=number] { width: 100%; }
#bmi-form input[type=submit] {
padding: 10px;
border: 0;
color: #fff;
background: #3e5eb7;
cursor: pointer;
}
#bmi-form {
border: 1px solid #eee;
background: #f2f2f2;
padding: 20px;
max-width: 400px;
}
#bmi-form .bmi-label {
color: #00a8c3;
font-weight: 700;
margin: 10px 0;
}
#bmi-form .bmi-row {
margin-bottom: 20px;
}
<!DOCTYPE html>
<html>
<head>
<title>Simple BMI Calculator</title>
<link rel="stylesheet" href="1-bmi.css">
<script src="2-bmi.js"></script>
</head>
<body>
<form id="bmi-form" onsubmit="return calcBMI();">
<div class="bmi-label">System:</div>
<div class="bmi-row">
<label>
<input type="radio" id="bmi-metric" name="bmi-measure" onchange="measureBMI()" checked> Metric
</label>
<label>
<input type="radio" id="bmi-imperial" name="bmi-measure" onchange="measureBMI()"> Imperial
</label>
</div>
<div class="bmi-label">
Weight (<span id="bmi-weight-unit">KG</span>):
</div>
<div class="bmi-row">
<input id="bmi-weight" type="number" min="1" max="635" required>
</div>
<div class="bmi-label">Height (<span id="bmi-height-unit">CM</span>):</div>
<div class="bmi-row">
<input id="bmi-height" type="number" min="54" max="272" required>
</div>
<input type="submit" value="Calculate BMI">
<span id="bmi-results"></span>
</form>
</body>
</html>
// (A) CHANGE BMI MEASURING SYSTEM
function measureBMI () {
// (A1) GET HTML ELEMENTS
let unit = document.getElementById("bmi-metric").checked,
weight = document.getElementById("bmi-weight"),
weightu = document.getElementById("bmi-weight-unit"),
height = document.getElementById("bmi-height"),
heightu = document.getElementById("bmi-height-unit");
// (A2) UPDATE HTML FORM FIELDS
// TRUE = METRIC, FALSE = IMPERIAL
if (unit) {
weightu.innerHTML = "KG";
weight.min = 1; weight.max = 635;
heightu.innerHTML = "CM";
height.min = 54; height.max = 272;
} else {
weightu.innerHTML = "LBS";
weight.min = 2; weight.max = 1400;
heightu.innerHTML = "IN";
height.min = 21; height.max = 107;
}
}
// (B) CALCULATE BMI
function calcBMI () {
// (B1) GET HTML ELEMENTS
let bmi = null,
unit = document.getElementById("bmi-metric").checked,
weight = parseInt(document.getElementById("bmi-weight").value),
height = parseInt(document.getElementById("bmi-height").value),
results = document.getElementById("bmi-results");
// (B2) CALCULATE BMI
// METRIC BMI = MASS (KG) / HEIGHT (M) SQUARE
if (unit) {
height = height / 100;
bmi = weight / (height * height);
}
// IMPERIAL BMI = 703 X MASS (LBS) / HEIGHT (IN) SQUARE
else {
bmi = 703 * (weight / (height * height));
}
// ROUND OFF - 2 DECIMAL PLACES
bmi = Math.round(bmi * 100) / 100;
// (B3) SHOW RESULTS
if (bmi < 18.5) {
results.innerHTML = bmi + " - Underweight";
} else if (bmi < 25) {
results.innerHTML = bmi + " - Normal weight";
} else if (bmi < 30) {
results.innerHTML = bmi + " - Pre-obesity";
} else if (bmi < 35) {
results.innerHTML = bmi + " - Obesity class I";
} else if (bmi < 40) {
results.innerHTML = bmi + " - Obesity class II";
} else {
results.innerHTML = bmi + " - Obesity class III";
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment