CSDT BLOG

DISCOVER COLLECTIONS AND BLOGS THAT MATCH YOUR INTERESTS.




Share ⇓




How to Create BMP Calculator in Javascript

Bookmark

How to Create BMP Calculator in Javascript

Creating a Body Mass Index (BMI) calculator in JavaScript involves taking the user's height and weight as inputs, calculating the BMI using the formula, and then displaying the result along with a category (e.g., underweight, normal weight, overweight, etc.).

Here's a simple example:  HTML

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>BMI Calculator</title>

    <style>

        body {

            font-family: Arial, sans-serif;

            background-color: #f4f4f4;

            text-align: center;

            margin-top: 50px;

        }


        .calculator {

            background-color: #fff;

            padding: 20px;

            border-radius: 10px;

            box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);

            width: 300px;

            margin: 0 auto;

        }


        input {

            padding: 10px;

            margin: 10px 0;

            width: 100%;

            box-sizing: border-box;

        }


        button {

            padding: 10px;

            width: 100%;

            background-color: #28a745;

            color: #fff;

            border: none;

            border-radius: 5px;

            cursor: pointer;

        }


        button:hover {

            background-color: #218838;

        }


        .result {

            margin-top: 20px;

            font-size: 18px;

        }

    </style>

</head>

<body>

    <div class="calculator">

        <h2>BMI Calculator</h2>

        <input type="number" id="weight" placeholder="Enter weight in kg">

        <input type="number" id="height" placeholder="Enter height in cm">

        <button onclick="calculateBMI()">Calculate BMI</button>

        <div class="result" id="bmiResult"></div>

    </div>


    <script src="script.js"></script>

</body>

</html>


JavaScript (script.js)
function calculateBMI() {
    // Get the input values
    const weight = document.getElementById('weight').value;
    const height = document.getElementById('height').value;

    // Validate inputs
    if (weight === "" || height === "") {
        alert("Please enter both weight and height.");
        return;
    }

    // Convert height from cm to meters
    const heightInMeters = height / 100;

    // Calculate BMI
    const bmi = weight / (heightInMeters * heightInMeters);

    // Determine BMI category
    let category = "";
    if (bmi < 18.5) {
        category = "Underweight";
    } else if (bmi >= 18.5 && bmi < 24.9) {
        category = "Normal weight";
    } else if (bmi >= 25 && bmi < 29.9) {
        category = "Overweight";
    } else {
        category = "Obese";
    }

    // Display the result
    document.getElementById('bmiResult').textContent = 
        "Your BMI is " + bmi.toFixed(2) + " (" + category + ")";
}

Explanation:

  1. HTML Structure:

    • A simple form with two input fields (weight and height) and a button to calculate BMI.
    • A div with the class result where the BMI and its category will be displayed.
  2. CSS Styling:

    • Basic styling to make the form look neat and centered.
    • The button changes color when hovered to improve user interaction.
  3. JavaScript Functionality:

    • The calculateBMI function is triggered when the user clicks the "Calculate BMI" button.
    • It retrieves the user's weight and height from the input fields.
    • The height is converted from centimeters to meters.
    • The BMI is calculated using the formula:BMI=weight (kg)height (m)2\text{BMI} = \frac{\text{weight (kg)}}{\text{height (m)}^2}
    • Based on the BMI value, a category is assigned (Underweight, Normal weight, Overweight, or Obese).
    • The BMI value (rounded to two decimal places) and the category are displayed on the page.

This example can be expanded with additional features, such as input validation to ensure only numbers are entered, or providing more detailed health advice based on the BMI category.

0

Our Recent Coment