0

抱歉,这显然是我第一次来这里,我只是在学习如何使用 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();
}
4

1 回答 1

0

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

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?

于 2012-08-28T02:21:03.540 回答