📜  使用 JavaScript 设计 BMI 计算器

📅  最后修改于: 2021-11-10 04:23:37             🧑  作者: Mango

体重指数 (BMI) 计算器可用于根据身高和体重计算 BMI 值。对于大多数人来说,BMI 是一个相当可靠的体脂指标。

公式:

BMI = (weight) / (height * height)

方法: BMI 是根据个人的体重和身高计算得出的数字。为了找出 BMI,我们将从用户那里获取输入(包括身高和体重),这些输入将存储在身高和体重变量中以供进一步计算。计算过程很简单,我们只需将体重(公斤)除以身高的平方即可。现在根据计算出的 BMI,它将执行相应的 if-else 语句。我们还检查用户是否在没有输入输入的情况下按下提交按钮,在这种情况下,我们正在打印提供身高或提供体重。
使用 HTML,我们提供了所需的结构、输入选项和提交按钮。在 CSS 的帮助下,我们通过提供颜色和所需的字体等来美化我们的结构。

在 JavaScript 部分,我们正在处理获取的输入,并在计算后打印相应的输出。

例子:

HTML:

index.html


  

    
    

  

    
        

BMI Calculator

                    

Height (in cm)

                       

Weight (in kg)

                                   
    
  


app.js
window.onload = () => {
    let button = document.querySelector("#btn");
  
    // Function for calculating BMI
    button.addEventListener("click", calculateBMI);
};
  
function calculateBMI() {
  
    /* Getting input from user into height variable.
    Input is string so typecasting is necessary. */
    let height = parseInt(document
            .querySelector("#height").value);
  
    /* Getting input from user into weight variable. 
    Input is string so typecasting is necessary.*/
    let weight = parseInt(document
            .querySelector("#weight").value);
  
    let result = document.querySelector("#result");
  
    // Checking the user providing a proper
    // value or not
    if (height === "" || isNaN(height)) 
        result.innerHTML = "Provide a valid Height!";
  
    else if (weight === "" || isNaN(weight)) 
        result.innerHTML = "Provide a valid Weight!";
  
    // If both input is valid, calculate the bmi
    else {
  
        // Fixing upto 2 decimal places
        let bmi = (weight / ((height * height) 
                            / 10000)).toFixed(2);
  
        // Dividing as per the bmi conditions
        if (bmi < 18.6) result.innerHTML =
            `Under Weight : ${bmi}`;
  
        else if (bmi >= 18.6 && bmi < 24.9) 
            result.innerHTML = 
                `Normal : ${bmi}`;
  
        else result.innerHTML =
            `Over Weight : ${bmi}`;
    }
}


JavaScript:

应用程序.js

window.onload = () => {
    let button = document.querySelector("#btn");
  
    // Function for calculating BMI
    button.addEventListener("click", calculateBMI);
};
  
function calculateBMI() {
  
    /* Getting input from user into height variable.
    Input is string so typecasting is necessary. */
    let height = parseInt(document
            .querySelector("#height").value);
  
    /* Getting input from user into weight variable. 
    Input is string so typecasting is necessary.*/
    let weight = parseInt(document
            .querySelector("#weight").value);
  
    let result = document.querySelector("#result");
  
    // Checking the user providing a proper
    // value or not
    if (height === "" || isNaN(height)) 
        result.innerHTML = "Provide a valid Height!";
  
    else if (weight === "" || isNaN(weight)) 
        result.innerHTML = "Provide a valid Weight!";
  
    // If both input is valid, calculate the bmi
    else {
  
        // Fixing upto 2 decimal places
        let bmi = (weight / ((height * height) 
                            / 10000)).toFixed(2);
  
        // Dividing as per the bmi conditions
        if (bmi < 18.6) result.innerHTML =
            `Under Weight : ${bmi}`;
  
        else if (bmi >= 18.6 && bmi < 24.9) 
            result.innerHTML = 
                `Normal : ${bmi}`;
  
        else result.innerHTML =
            `Over Weight : ${bmi}`;
    }
}

输出: