www.webdeveloper.com
Results 1 to 11 of 11

Thread: New User Looking for help with applying a discount in to a value based on user input

  1. #1
    Join Date
    Jul 2013
    Posts
    3

    New User Looking for help with applying a discount in to a value based on user input

    Trying to apply a discount to the calculate function. I can't seem to get the function to work when adding the function I have started to make with a if else layout comparing the form fields with an if statement giving the discount value. Any help would be great.

    Code:
    /*
    This source is shared under the terms of LGPL 3
    www.gnu.org/licenses/lgpl.html
    
    You are free to use the code in Commercial or non-commercial projects
    */
     var papersize_prices = new Array();
     papersize_prices["6.25x9"]=.25;
     papersize_prices["6.25x11"]=.33;
     papersize_prices["4x12"]=.25;
     papersize_prices["8.5x7"]=.50;
     papersize_prices["8.5x11"]=.50;
     papersize_prices["12x15"]=1;
     
    
    
     //Set up an associative array
     //The keys represent the size of the cake
     //The values represent the cost of the cake i.e A 10" cake cost's $35
     var cake_prices = new Array();
     cake_prices["100"]=100;
     cake_prices["250"]=250;
     cake_prices["500"]=500;
     cake_prices["1000"]=1000;
     cake_prices["2000"]=2000;
     cake_prices["5000"]=5000;
     cake_prices["7000"]=7000;
     cake_prices["10000"]=10000;
     cake_prices["15000"]=15000;
     cake_prices["20000"]=20000;
     cake_prices["30000"]=30000;
     cake_prices["50000"]=50000;
      
     
     //Set up an associative array 
     //The keys represent the filling type
     //The value represents the cost of the filling i.e. Lemon filling is $5,Dobash filling is $9
     //We use this this array when the user selects a filling from the form
     var filling_prices= new Array();
     filling_prices["None"]=0;
     filling_prices["80lb_Cover"]=1;
     filling_prices["100lb_Cover"]=2;
     filling_prices["120lb_Cover"]=3;
     filling_prices["80lb_Gloss/Matted"]=4;
     filling_prices["100lb_Gloss/Matted"]=5;
     filling_prices["120lb_Gloss/Matted"]=6;
     
     var mail_prices= new Array();
     mail_prices["None"]=0;
     mail_prices["EDDMREADY"]=10;
     
     
     
    
    function getCakeSizePrice()
    {
        var cakeSizePrice=0;
        //Get a reference to the form id="cakeform"
        var theForm = document.forms["cakeform"];
        //Get a reference to the select id="qty"
         var selectedCake = theForm.elements["selectedcake"];
         
        //set cakeFilling Price equal to value user chose
        //For example filling_prices["Lemon".value] would be equal to 5
    	cakeSizePrice = cake_prices[selectedCake.value];
        //finally we return cakeFillingPrice
        return cakeSizePrice;
    }
    
    function getPaperSizePrice()
    {
        var paperSizePrice=0;
        //Get a reference to the form id="cakeform"
        var theForm = document.forms["cakeform"];
        //Get a reference to the select id="qty"
         var selectedPaper = theForm.elements["PaperSize"];
         
        //set cakeFilling Price equal to value user chose
        //For example filling_prices["Lemon".value] would be equal to 5
    	paperSizePrice = papersize_prices[selectedPaper.value];
        //finally we return cakeFillingPrice
        return paperSizePrice;
    }
    
    function getdiscount()
    {
     var discount = 0;
     var selectedPaperSize = theForm.elements["PaperSize"];
     paperSizePrice = papersize_prices[selectedPaperSize.value];
     var selectedCake = theForm.elements["selectedcake"];
     cakeSizePrice = cake_prices[selectedCake.value];
     var selectedFilling = theForm.elements["filling"];
     cakeFillingPrice = filling_prices[selectedFilling.value];
     
     if (paperSizePrice ="500")
     {
    	 if (cakeSizePrice="6.25x9")
    	 {
    		 if (cakeFillingPrice="80lb_Cover")
    		 discount =".20";
    	 }
     return discount;
     }
    }
    //This function finds the filling price based on the 
    //drop down selection
    function getFillingPrice()
    {
        var cakeFillingPrice=0;
        //Get a reference to the form id="cakeform"
        var theForm = document.forms["cakeform"];
        //Get a reference to the select id="filling"
         var selectedFilling = theForm.elements["filling"];
         
        //set cakeFilling Price equal to value user chose
    
        cakeFillingPrice = filling_prices[selectedFilling.value];
    
        //finally we return cakeFillingPrice
        return cakeFillingPrice;
    }
    
    function getoption1price()
    {
        var option1price=0;
        //Get a reference to the form id="cakeform"
        var theForm = document.forms["cakeform"];
        //Get a reference to the select id="option1"
         var option1price = theForm.elements["mail"];
         
        //set cakeFilling Price equal to value user chose
        //For example filling_prices["Lemon".value] would be equal to 5
    	option1price = mail_prices[option1price.value];
        //finally we return cakeFillingPrice
        return option1price;
    }
    
    //candlesPrice() finds the candles price based on a check box selection
    function candlesPrice()
    {
        var candlePrice=0;
        //Get a reference to the form id="cakeform"
        var theForm = document.forms["cakeform"];
        //Get a reference to the checkbox id="includecandles"
        var includeCandles = theForm.elements["includecandles"];
    
        //If they checked the box set candlePrice to 5
        if(includeCandles.checked==true)
        {
            candlePrice=5;
        }
        //finally we return the candlePrice
        return candlePrice;
    }
    
    function insciptionPrice()
    {
        //This local variable will be used to decide whether or not to charge for the inscription
        //If the user checked the box this value will be 20
        //otherwise it will remain at 0
        var inscriptionPrice=0;
        //Get a refernce to the form id="cakeform"
        var theForm = document.forms["cakeform"];
        //Get a reference to the checkbox id="includeinscription"
        var includeInscription = theForm.elements["includeinscription"];
        //If they checked the box set inscriptionPrice to 20
        if(includeInscription.checked==true){
            inscriptionPrice=20;
        }
     
        return inscriptionPrice;
    }
            
    function calculateTotal()
    {
        //Here we get the total price by calling our function
        //Each function returns a number so by calling them we add the values they return together
        var cakePrice = getPaperSizePrice() * getCakeSizePrice() * getFillingPrice() + candlesPrice() + insciptionPrice() + getoption1price();
    
        
        //display the result
        var divobj = document.getElementById('totalPrice');
        divobj.style.display='block';
        divobj.innerHTML = "Total Price For the Order $"+cakePrice;
    
    }
    
    function hideTotal()
    {
        var divobj = document.getElementById('totalPrice');
        divobj.style.display='none';
    }

    Form page

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,441
    Make it easier on the one's who wish to help your and provide the HTML with any CSS, if applicable.

  3. #3
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,276
    Javascript arrays are indexed only with integers. Not with values...
    Try the following code to see the differences with objects
    Code:
    var papersize_prices=new Array();// or []
    papersize_prices["6.25x9"]=.25;
    alert(papersize_prices);// => nothing 
    
    var cake_prices=[];
    cake_prices["1000"]=1000;
    alert(cake_prices);// => an array with 999 undefined values and a 1000th value=1000
    
    var cake_prices=new Object();// or {}
    cake_prices["1000"]=1000;
    alert(JSON.stringify(cake_prices)) //=>an associative array
    Last edited by 007Julien; 08-01-2013 at 05:06 AM.

  4. #4
    Join Date
    Jul 2013
    Posts
    3
    /*
    This source is shared under the terms of LGPL 3
    www.gnu.org/licenses/lgpl.html

    You are free to use the code in Commercial or non-commercial projects
    */
    var papersize_prices = new Array();
    papersize_prices["6.25x9"]=.25;
    papersize_prices["6.25x11"]=.33;
    papersize_prices["4x12"]=.25;
    papersize_prices["8.5x7"]=.50;
    papersize_prices["8.5x11"]=.50;
    papersize_prices["12x15"]=1;
    papersize_prices[1]=.25;
    papersize_prices[2]=.33;


    //Set up an associative array
    //The keys represent the size of the cake
    //The values represent the cost of the cake i.e A 10" cake cost's $35
    var cake_prices = new Array();
    cake_prices["500"]=500;
    cake_prices["1000"]=1000;
    cake_prices["2000"]=2000;
    cake_prices["5000"]=5000;
    cake_prices["7000"]=7000;
    cake_prices["10000"]=10000;
    cake_prices["15000"]=15000;
    cake_prices["20000"]=20000;
    cake_prices["30000"]=30000;
    cake_prices["50000"]=50000;
    cake_prices[1]=500;
    cake_prices[2]=1000;

    //Set up an associative array
    //The keys represent the filling type
    //The value represents the cost of the filling i.e. Lemon filling is $5,Dobash filling is $9
    //We use this this array when the user selects a filling from the form
    var paperstock_prices= new Array();
    paperstock_prices["None"]=0;
    paperstock_prices["80lb_Cover"]=.01;
    paperstock_prices["100lb_Cover"]=.01;
    paperstock_prices["120lb_Cover"]=.02;
    paperstock_prices["80lb_Gloss/Matted"]=.01;
    paperstock_prices["100lb_Gloss/Matted"]=.01;
    paperstock_prices["120lb_Gloss/Matted"]=.02;
    paperstock_prices[1]=.01;
    paperstock_prices[2]=.02;

    var mail_prices= new Array();
    mail_prices["None"]=0;
    mail_prices["EDDMREADY"]=10;



    function getCakeSizePrice()
    {
    var cakeSizePrice=0;
    //Get a reference to the form id="cakeform"
    var theForm = document.forms["cakeform"];
    //Get a reference to the select id="qty"
    var selectedCake = theForm.elements["selectedcake"];

    //set cakeFilling Price equal to value user chose
    //For example filling_prices["Lemon".value] would be equal to 5
    cakeSizePrice = cake_prices[selectedCake.value];
    //finally we return cakeFillingPrice
    return cakeSizePrice;
    }

    function getPaperSizePrice()
    {
    var paperSizePrice=0;
    //Get a reference to the form id="cakeform"
    var theForm = document.forms["cakeform"];
    //Get a reference to the select id="qty"
    var selectedPaper = theForm.elements["PaperSize"];

    //set cakeFilling Price equal to value user chose
    //For example filling_prices["Lemon".value] would be equal to 5
    paperSizePrice = papersize_prices[selectedPaper.value];
    //finally we return cakeFillingPrice
    return paperSizePrice;
    }

    function getdiscount()
    {
    var discount = 0;

    if(papersize_prices["6.25x9"] && cake_prices["500"] && paperstock_prices["80lb_Cover"])
    discount ="0";
    if(papersize_prices["6.25x11"] && cake_prices["500"] && paperstock_prices["80lb_Cover"])
    discount =".10";
    if(papersize_prices["4x12"] && cake_prices["500"] && paperstock_prices["80lb_Cover"])
    discount =".20";
    if(papersize_prices[1] && cake_prices[1] && paperstock_prices[1])
    discount =".50";
    if(papersize_prices[1] && cake_prices[2] && paperstock_prices[1])
    discount =".100";

    return discount;
    }
    //This function finds the filling price based on the
    //drop down selection
    function getPaperStockPrice()
    {
    var paperStockPrice=0;
    //Get a reference to the form id="cakeform"
    var theForm = document.forms["cakeform"];
    //Get a reference to the select id="filling"
    var selectedPaperStock = theForm.elements["paperstock"];

    //set cakeFilling Price equal to value user chose
    //For example filling_prices["Lemon".value] would be equal to 5
    paperStockPrice = paperstock_prices[selectedPaperStock.value];

    //finally we return cakeFillingPrice
    return paperStockPrice;
    }

    function getoption1price()
    {
    var option1price=0;
    //Get a reference to the form id="cakeform"
    var theForm = document.forms["cakeform"];
    //Get a reference to the select id="option1"
    var option1price = theForm.elements["mail"];

    //set cakeFilling Price equal to value user chose
    //For example filling_prices["Lemon".value] would be equal to 5
    option1price = mail_prices[option1price.value];
    //finally we return cakeFillingPrice
    return option1price;
    }

    //candlesPrice() finds the candles price based on a check box selection
    function candlesPrice()
    {
    var candlePrice=0;
    //Get a reference to the form id="cakeform"
    var theForm = document.forms["cakeform"];
    //Get a reference to the checkbox id="includecandles"
    var includeCandles = theForm.elements["includecandles"];

    //If they checked the box set candlePrice to 5
    if(includeCandles.checked==true)
    {
    candlePrice=5;
    }
    //finally we return the candlePrice
    return candlePrice;
    }

    function insciptionPrice()
    {
    //This local variable will be used to decide whether or not to charge for the inscription
    //If the user checked the box this value will be 20
    //otherwise it will remain at 0
    var inscriptionPrice=0;
    //Get a refernce to the form id="cakeform"
    var theForm = document.forms["cakeform"];
    //Get a reference to the checkbox id="includeinscription"
    var includeInscription = theForm.elements["includeinscription"];
    //If they checked the box set inscriptionPrice to 20
    if(includeInscription.checked==true){
    inscriptionPrice=20;
    }
    //finally we return the inscriptionPrice
    return inscriptionPrice;
    }

    function calculateTotal()
    {
    //Here we get the total price by calling our function
    //Each function returns a number so by calling them we add the values they return together
    var basepreprice = getCakeSizePrice() * (getPaperSizePrice() + getPaperStockPrice());

    var orderdiscount = basepreprice * getdiscount();
    var cakePrice = basepreprice - orderdiscount + candlesPrice() + insciptionPrice() + getoption1price();


    //display the result
    var divobj = document.getElementById('totalPrice');
    divobj.style.display='block';
    divobj.innerHTML = "Total Price For the Order $"+cakePrice;

    }

    function hideTotal()
    {
    var divobj = document.getElementById('totalPrice');
    divobj.style.display='none';
    }

  5. #5
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,276
    This code is partially wrong with or without licence !

  6. #6
    Join Date
    Jul 2013
    Posts
    3
    Quote Originally Posted by 007Julien View Post
    This code is partially wrong with or without licence !
    why??

  7. #7
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,276
    Read and test my preceding post or see this page javascript Array object of w3scholls.com
    Last edited by 007Julien; 08-01-2013 at 02:11 PM.

  8. #8
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,507
    Quote Originally Posted by 007Julien View Post
    Javascript arrays are indexed only with integers. Not with values...
    Try the following code to see the differences with objects
    Code:
    var papersize_prices=new Array();// or []
    papersize_prices["6.25x9"]=.25;
    alert(papersize_prices);// => nothing 
    
    var cake_prices=[];
    cake_prices["1000"]=1000;
    alert(cake_prices);// => an array with 999 undefined values and a 1000th value=1000
    
    var cake_prices=new Object();// or {}
    cake_prices["1000"]=1000;
    alert(JSON.stringify(cake_prices)) //=>an associative array
    They are referenced by either integers or names.

    In your example you would get nothing because papersizes_prices is an array, therefore you need to add ["6.25x9"] to the end to get a popup that says 0.25
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  9. #9
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,507
    Having the HTML does help.

    IMHO your script is over complicated and without seeing how it relates to the form your using, it is anyones guess.

    It is likely that you could store some of the data you are storing in Javascript in the web form and cut down on your JS. We won't know until the script and HTML are seen together.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  10. #10
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,276
    Sorry.
    You are partially right.
    An alert(papersize_prices["6.25x9"]) give the value 0.25 after a papersize_prices["6.25x9"]=0.25;

    But papersize_prices is no more an Array. It as no more length (an alert(papersize_prices.length) gives 0) nor toString() method (which is called with an alert() an give a result with cakes_prices) !
    Last edited by 007Julien; 08-01-2013 at 03:09 PM.

  11. #11
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,276
    Nothing to add.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles