Skip to content

Instantly share code, notes, and snippets.

@kpostekk
Created March 11, 2019 17:12
Show Gist options
  • Save kpostekk/9d2c8b1ba522dd04f3da3f1925beff66 to your computer and use it in GitHub Desktop.
Save kpostekk/9d2c8b1ba522dd04f3da3f1925beff66 to your computer and use it in GitHub Desktop.
Simple BMI calculator
function in_range(x, min, max) {
return x >= min && x <= max;
}
function return_bmi(weight, height) {
// weight should be in kilograms
// height should be in meters
return weight / (height ** 2);
}
function specify_bmi(bmi_value) {
if (bmi_value < 18.5) {
return 'niedowagę';
} else if (in_range(bmi_value, 18.5, 24.9)) {
return 'wagę prawidłową';
} else if (in_range(bmi_value, 25, 29.9)) {
return 'nadwagę (no jesteś ulanicem)';
} else if (bmi_value > 30) {
return 'otyłość'
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Kalkulator BMI</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="calculator.js"></script>
<script src="init.js"></script>
</head>
<body>
<div class="container">
<h1 class="text-center">Kalkulator BMI</h1>
<div class="row">
<div class="col-lg-6">
<p><label for="height">Wzrost (w metrach)</label>
<input style="width: 300px" type="number" id="height" class="form-control" value="1.7" step="0.05">
</p>
<p>
<label for="weight">Waga (w kilogramach)</label>
<input style="width: 300px" type="number" id="weight" class="form-control" value="60" step="0.5">
</p>
</div>
<div class="col-lg-6">
<h2>Twoje BMI to: <span id="result" class="text-primary">20.76</span></h2>
<h2>Oznacza to, że masz <span id="result2" class="text-primary">wagę prawidłową</span></h2>
</div>
</div>
</div>
</body>
</html>
function update_calc() {
height = parseFloat($('#height').val());
weight = parseFloat($('#weight').val());
bmi_value_result = return_bmi(weight, height);
console.log(bmi_value_result);
$('#result').text(
bmi_value_result.toFixed(2).toString()
);
$('#result2').text(
specify_bmi(
bmi_value_result
)
)
}
$(document).on('change', '#weight',
function () {
update_calc()
}
);
$(document).on('change', '#height',
function () {
update_calc()
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment