2012-08-28 25 views
0

对不起,这显然是我第一次在这里,我只是学习如何在JavaScript中工作。我的问题是这样的:我有一些基本计算来确定我们的非营利组织的服务价格。 t是房间数量* 0.81。但我们每月至少需要60美元。所以我需要知道如何将其纳入定价功能。我知道它会说“如果60,然后60”,就不知道该怎么写。我将包括完整的js。我有一个领域的计算,并需要显示最小值,如果结果低于60

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(); 
} 
+2

你能更好地解释这些变量的含义和你想要的吗? – Oriol

+1

什么是checkCount?什么是费率?什么是rmCnt? –

+0

你还没有问过问题,所以我们不清楚如何提供帮助。请编辑你的问题,问问题,并告诉我们你看到了什么以及它是错误的。 – walrii

回答

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?

+0

因此,在我的脚本中,我会在哪里放置它,以及myvar应该更改为什么? –

+0

@JeremyChambers哪个值是你想要的至少60? – Oriol

+0

var calculatePrice –

相关问题