/    Sign up×
Community /Pin to ProfileBookmark

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.25×9”]=.25;
papersize_prices[“6.25×11”]=.33;
papersize_prices[“4×12”]=.25;
papersize_prices[“8.5×7”]=.50;
papersize_prices[“8.5×11”]=.50;
papersize_prices[“12×15″]=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.25×9″)
{
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’;
}
[/CODE]

Form page

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERAug 01.2013 — Make it easier on the one's who wish to help your and provide the HTML with any CSS, if applicable.
Copy linkTweet thisAlerts:
@007JulienAug 01.2013 — 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
[/CODE]
Copy linkTweet thisAlerts:
@funinsunauthorAug 01.2013 — /*

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';

}
Copy linkTweet thisAlerts:
@007JulienAug 01.2013 — This code is partially wrong with or without licence !
Copy linkTweet thisAlerts:
@funinsunauthorAug 01.2013 — This code is partially wrong with or without licence ![/QUOTE]

why??
Copy linkTweet thisAlerts:
@007JulienAug 01.2013 — Read and test my preceding post or see this page javascript Array object of w3scholls.com
Copy linkTweet thisAlerts:
@rootAug 01.2013 — 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
[/CODE]
[/QUOTE]


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
Copy linkTweet thisAlerts:
@rootAug 01.2013 — 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.
Copy linkTweet thisAlerts:
@007JulienAug 01.2013 — 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) !
Copy linkTweet thisAlerts:
@007JulienAug 01.2013 — Nothing to add.
×

Success!

Help @funinsun spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...