简体   繁体   English

为什么我的计算 function 不能正常运行?

[英]Why does my calculation function not run properly?

I need to create a BAC calculator in JavaScript. When I run the code, the form validation works but the calculate function doesn't seem to run.我需要在 JavaScript 中创建一个 BAC 计算器。当我运行代码时,表单验证有效但计算 function 似乎没有运行。

    function validateForm() {
    let weight = document.getElementById("weight").value;
    let beer = document.getElementById("beer").value;
    let wine = document.getElementById("wine").value;
    let liquor = document.getElementById("liquor").value;
    let hours = document.getElementById("hours").value;
    if (weight <= 0){
        alert("Weight must be more than zero")
        return false;
    }

    else if (beer<0 || wine<0 || liquor<0 || hours<0){
        alert("Number must be zero or higher")
        return false;
    }
    else if(isNaN(weight)||isNaN(beer)||isNaN(wine)||isNaN(liquor)||isNaN(hours)){
        alert("Answer must be a number")
        return false;
    }
    else if (weight == null||beer == null||wine == null||liquor == null||hours == null){
        alert("All fields must be filled")
        return false;
    }
    else{
        return calculate;
    }
}
function calculate() {
    let weight = document.getElementById("weight").value;
    let beer = document.getElementById("beer").value;
    let wine = document.getElementById("wine").value;
    let liquor = document.getElementById("liquor").value;
    let hours = document.getElementById("hours").value;
    let answer = (((((beer * 0.54)+(wine*0.6)+(liquor*0.6))*7.5)/weight)-(hours*0.015)).toFixed(3)
    document.getElementById("bac").value = answer;
    document.getElementById("answer").append(answer);
    return answer;
}

If anyone has any answers I'd be more than happy.如果有人有任何答案,我会非常高兴。 Thank you.谢谢你。

In the validateForm() -method, try return calculate();validateForm()方法中,尝试return calculate();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM