函数公式并不适用

时间:2014-03-30 19:21:16

标签: javascript jquery

我有这个公式,我无法理解为什么它会给出错误的结果。这是代码:

//gebder 0=male 1=female

$(document).ready(function() {
$('#met_inputs').hide();
$('#unit_std').prop('checked', true);
var standardUnits = true;
$('input[name="unit"]:radio').change(function() {
    if ($(this).val() === "met") {
        standardUnits = false;
        $('#standard_inputs').hide();
        $('#met_inputs').show();
    } else {
        standardUnits = true;
        $('#standard_inputs').show();
        $('#met_inputs').hide();
    }
});
var BMI = 0;
var RMR = 0;
$("#cal_btn").click(function() {
    if (standardUnits) {
        BMI = BMIStd();
    } else {
        BMI = BMIMet();
    }


    BMI = BMI.toFixed(1);

    RMR = getRMR().toFixed(1);

    //make all inputs black
    $("input[type='text']").each(function() {
        $(this).css('border-color', 'black');
    });


    if (isNaN(BMI) || isNaN(RMR)) {
        $('#results').html('<h2>Please fill-up the required field.</h2>');
        $('#results').css('color', 'red');

    //make blank inputs red
        $("input[type='text']").each(function() {
            if ($(this).val() === '') {
                $(this).css('border-color', 'red');
            }
        });
        return;
    } else {
        $('#results').css('color', 'black');
    }

    $("#results").html("<h2>Your BMI is " + BMI+"</h2>");

    if (BMI > 25) {
        $('#results').append("You are <b>above</b> the Normal BMI Range. Lose at least <b>" +    getLoss().toFixed(1) + "</b> to reach Normal BMI Range. Read these <a href='http://www.calculator-bmi.com/category/how-to-lose-weight/'>posts that will help you to lose weight. </a>");

 //$('#suggestion').append("<br><br>Your Resting Metabolic Rate (<i>Calories your body use</i>) is = <b>"+RMR+"</b>. This will be your guide if you want to lose, gain or maintain your weight.");
    } else if (BMI < 25 && BMI > 18.5) {
        $('#results').append("You weight is great! Just maintain what you're doing. Read more on <a href='http://www.calculator-bmi.com/category/how-to-maintain-weight/'>How to Maintain Weight</a> for optimal health.");
    } else if (BMI <= 18.4) {
        $('#results').append("You are <b>below</b> the Normal BMI Range. Gain at least <b>" + getGain().toFixed(1) + "</b> to reach Normal BMI Range. Read these <a href='http://www.calculator-bmi.com/category/how-to-gain-weight/'>posts on How to Gain Weight. </a>");

    }
    $('#suggestion').html("Your <b>Basal Metabolic Rate</b> (Calories your body use) is <b>" + RMR + "</b>. This will be your guide if you want to lose, gain or maintain your weight.");
});


function BMIMet() {
    var weight = parseFloat($("#met_weight").val());
    var height = parseFloat($("#met_height").val());
    return weight * 10000 / Math.pow(height, 2);
}

function BMIStd() {
    var weight = parseFloat($("#std_weight").val());
    var height = parseFloat($("#std_height_ft").val()) * 12 + parseFloat($("#std_height_in").val());
    return weight * 703 / Math.pow(height, 2);
}

function getRMR() {
    var weight = 0;
    var height = 0;
    var age = parseFloat($('#age').val());
    if (standardUnits) {
        weight = parseFloat($("#std_weight").val());
        height = parseFloat($("#std_height_ft").val()) * 12 + parseFloat($("#std_height_in").val());
    } else {
        weight = LbtoKg(parseFloat($("#met_weight").val()));
        height = IntoCm(parseFloat($("#met_height").val()));
    }



    if (getGender() === '0') {
        return (66 + (13.7 * weight) + (5 * height)) - (6.8 * age);
    } else {
        return 655 + (9.6 * weight) + (1.8 * height) - (4.7 * age);
    }
}

function getLoss() {
    if (standardUnits) {
        weight = parseFloat($("#std_weight").val());
        height = parseFloat($("#std_height_ft").val()) * 12 + parseFloat($("#std_height_in").val());
        return weight - (25 / 703) * height * height;
    } else {
        weight = parseFloat($("#met_weight").val());
        height = parseFloat($("#met_height").val());
        return weight - (25 / 10000) * height * height;
    }
}

function getGain() {
    if (standardUnits) {
        weight = parseFloat($("#std_weight").val());
        height = parseFloat($("#std_height_ft").val()) * 12 + parseFloat($("#std_height_in").val());
        return ((18.5 / 703) * height * height) - weight;
    } else {
        weight = parseFloat($("#met_weight").val());
        height = parseFloat($("#met_height").val());
        return ((18.5 / 10000) * height * height) - weight;
    }
}

function LbtoKg(val) {
    return parseFloat(val) * 2.20462;
}

function IntoCm(val) {
    return parseFloat(val) * 0.393701;
}

function getGender() {
    return $('select[name="gender"]').val();
}



});

这是正确的公式。

女性:BMR = 655 +(4.35 x体重磅)+(4.7 x身高(英寸)) - (4.7 x年龄)

男子:BMR = 66 +(6.23 x体重磅)+(12.7 x身高(英寸)) - (6.8 x年龄)

当我使用计算器时,它显示的结果不正确。我不知道如何解决它。而且我在编码方面也不是那么好。我希望有人可以提供帮助。

以下是该代码中包含公式的部分..

    if (getGender() === '0') {
        return (66 + (13.7 * weight) + (5 * height)) - (6.8 * age);
    } else {
        return 655 + (9.6 * weight) + (1.8 * height) - (4.7 * age);
    }

0 个答案:

没有答案