Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8449361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:41:00+00:00 2026-06-10T10:41:00+00:00

Sorry this is obviously my first time here, I am just learning how to

  • 0

Sorry this is obviously my first time here, I am just learning how to work in javascript. My question is this: I have some basic calculations determing a price of a service for our non-profit. t is the number of rooms * 0.81. But we have a monthly minimum of $60. So I need to know how I would factor that into the pricing function. I know it goes that “if x < 60, then 60”, just not sure how the language would be written. I will include the full 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();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T10:41:02+00:00Added an answer on June 10, 2026 at 10:41 am

    If you only want to set a minimum value of 60 to the variable myvar, you can do

    myvar=Math.max(60,myvar);
    

    Edit:

    Then, if you want the value returned by calculatePrice to be at least 60, use:

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

    Note 1:

    Do you know that you can declare functions like this?

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

    It’s shorter and this way you can call the function before declaring it!

    Note 2:

    If you want that value to be at least 60, I don’t understand the following code:

    checkCount(rmCnt) ? 0
    

    Why 0?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry this is a basic question, but all my research just barely missed answering
this is my first time posting a question here - I've searched for ones
Using MVC3 for the first time, so sorry for the noob-like question. I'm trying
This is my first question here so please bear with me - I apologise
Sorry if this is a comp-sci 101 question. I'm just unsure if I'm missing
I know this may be a very basic question, sorry if it is but
Sorry if this is obvious but have looked around and can't get this working:
Sorry this is not a very well defined question, I am thinking about an
Sorry this is probably super basic. But in all my javabean examples, I've not
sorry this is such a simple question but I can't figure it out. How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.