我有一个字段计算,如果结果低于 60,则需要显示最小值

I have a field calculating and need to display a minimum if the result is below 60

本文关键字:最小值 显示 字段 有一个 计算 如果 结果      更新时间:2023-09-26

对不起,这显然是我第一次来这里,我只是在学习如何使用javascript。我的问题是:我有一些基本的计算来确定我们非营利组织的服务价格。t 是房间数 * 0.81。但我们每月最低为 60 美元。所以我需要知道如何将其纳入定价功能。我知道"如果x<60,那么60",只是不确定语言将如何编写。我将包括完整的js。

var listenerFieldIDs = {"roomCountID":"item4_text_1"}; //Currently the only form we are using for room count has this value set as its ID attribute.
var impactFields = ["item12_text_1","item1_text_1","item16_text_1","item18_text_1","item20_text_1"]; //Field IDs for the form that will be changed constantly.
var estimatedBottleSize = 1.5, occupancyRate = (60 / 100), collectionDuration = 365, soapOuncesRecoverable = 0.63, bottleOuncesRecoverable = 0.47,lbConversion = 0.0626, rate = 0.81;
var $ = function(id){ //Shortcut to save some typing. Instead of having to write out document.getElementById(elementID) every time I need to access an element, I can put $(elementID).property or $(elementID).method() I need more easily.
    return document.getElementById(id);
}
var updateFormField = function(id,amount){ //Updates a form field when gives the element ID and the amount.
    $(id).value = amount;
}
var updateForm = function(roomCount){ 
    // This is called when all form data needs to be updated. This is generally invoked each time a keystroke in the room count field.
    updateFormField(impactFields[0],calculateLbsOfSoap(roomCount).toFixed(2)); //Updating the first form field after calculating the total weight of soap in lbs.
    updateFormField(impactFields[1],calculateLbsOfBottles(roomCount).toFixed(2)); //Same thing as above, but bottles/amenities.
    updateFormField(impactFields[2],calculateBarsOfSoap(roomCount).toFixed(0)); //Updating the third form field after calculating the total number of distributed units.
    updateFormField(impactFields[3],calculateBottles(roomCount).toFixed(0)); //Same as above, but bottles/amenities.
    updateFormField(impactFields[4],("$" + calculatePrice(roomCount).toFixed(2))); //Updating price.
}
var listenForNumbers = function(event){ //This function is acting as a handler for when anything is entered into the field.
    updateForm($(listenerFieldIDs["roomCountID"]).value);
}
var calculateLbsOfSoap = function (rmCnt){ // Calculate the weight of soap and return the amount.
    return checkCount(rmCnt) ? 0 : ((soapOuncesRecoverable * lbConversion) * (rmCnt * occupancyRate) * collectionDuration); 
}
var calculateLbsOfBottles = function (rmCnt){ // Calculate the weight of bottled amenities and return the amount.
    return checkCount(rmCnt) ? 0 : ((bottleOuncesRecoverable * lbConversion) * (rmCnt * occupancyRate) * collectionDuration);
}
var calculateBarsOfSoap = function(rmCnt){ // Calculate how many bars are distributed if the room count is not 0.
    return checkCount(rmCnt) ? 0 : ((calculateLbsOfSoap(rmCnt) * 16) / 3);
}
var calculateBottles = function(rmCnt){ // Calculate how many bottles are distributed if the room count is not 0.
    return checkCount(rmCnt) ? 0 : (((calculateLbsOfBottles(rmCnt) * 16) / estimatedBottleSize) * (2 / 3)); 
}
var calculatePrice = function(rmCnt){
    return checkCount(rmCnt) ? 0 : (rmCnt * rate);
    }

var checkCount = function(count){ //If the count is 0  or less than 0, the number is useless so just return 0 to prevent odd results.
    return (count < 0 || count == 0) ? true : false; 
}
var initializeRealTimeCalcToForm = function(){ 

    if(window.attachEvent){
        $(listenerFieldIDs["roomCountID"]).attachEvent("onkeydown",listenForNumbers,false);
        $(listenerFieldIDs["roomCountID"]).attachEvent("onkeyup",listenForNumbers,false);
        $(listenerFieldIDs["roomCountID"]).attachEvent("onkeypress",listenForNumbers,false);
        $(listenerFieldIDs["roomCountID"]).attachEvent("onchange",listenForNumbers,false);
    } else{
    //But if NOT IE... :-D
        $(listenerFieldIDs["roomCountID"]).addEventListener("keydown",listenForNumbers,false);
        $(listenerFieldIDs["roomCountID"]).addEventListener("keyup",listenForNumbers,false);
        $(listenerFieldIDs["roomCountID"]).addEventListener("keypress",listenForNumbers,false);
        $(listenerFieldIDs["roomCountID"]).addEventListener("change",listenForNumbers,false);
    }
}
window.onload = function(){  
    initializeRealTimeCalcToForm();
}

如果您只想为变量 myvar 设置最小值 60 ,您可以

myvar=Math.max(60,myvar);

编辑:

然后,如果您希望 calculatePrice 返回的值至少为 60,请使用:

var calculatePrice=function(rmCnt){
    return Math.max(60,checkCount(rmCnt) ? 0 : (rmCnt * rate));
}

注1:

你知道你可以声明这样的函数吗?

function calculatePrice(rmCnt){
    return Math.max(60,checkCount(rmCnt) ? 0 : (rmCnt * rate));
}

更短,这样您就可以在声明函数之前调用它!

注2:

如果您希望该值至少为 60,我不明白以下代码:

checkCount(rmCnt) ? 0

为什么是0?