Hi,
I’m using this code which I got of this site.
I want to learn javascript a bit and at the same time change up this code to be of use in a project. So far so good.
But now I’m stuck with this issue’s.
1: I have a seperately bagged checkbox which would multiply my total with 0.5.
I thought I would multiply the cakeSelect.value because this is the value that needs multiplying. Though this gives me no result. So I’m most likely using that return wrong.
//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 cupcake_prices = new Array();
cupcake_prices["cc12"]=12;
cupcake_prices["cc18"]=18;
cupcake_prices["cc24"]=24;
cupcake_prices["cc30"]=30;
cupcake_prices["cc36"]=36;
cupcake_prices["cc42"]=42;
cupcake_prices["cc48"]=48;
cupcake_prices["cc54"]=54;
cupcake_prices["cc60"]=60;
//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 topping_prices = new Array();
topping_prices["Standaard"]=1;
topping_prices["Boterroom"]=1.35;
topping_prices["Logo/Foto"]=1.45;
topping_prices["Boterroomfondant"]=1.6;
topping_prices["Afgewerkt"]=2.1;
// getCakeSizePrice() finds the price based on the size of the cake.
// Here, we need to take user's the selection from radio button selection
function getCupCakeSizePrice() {
var cakeRadio = document.getElementsByName('selectedcupcake');
for (i=0; i < cakeRadio.length; i++) {
if (cakeRadio[i].checked) {
user_input = cakeRadio[i].value;
}
}
return cupcake_prices[user_input];
}
// getFillingPrice() finds the price based on the filling of the cake.
// Here, we need to take user's the selection from selection list
function getToppingPrice() {
var cakeSelect = document.getElementById('topping');
//alert(filling_prices[cakeSelect.value]);
return topping_prices[cakeSelect.value];
}
// getCandlesPrice() finds the price based if Candles is selected or not.
function getSeperatlyPrice() {
var cakeSeperatly = document.getElementById('baggedseperatly');
if(cakeSeperatly.checked) {
return(cakeSelect.value * 5);
} else {
return(0);
}
}
// getInscriptionPrice() finds the price based if Inscription is selected or not.
function getInscriptionPrice() {
var cakeInscription = document.getElementById('includeinscription');
if(cakeInscription.checked) {
return(20);
} else {
return(0);
}
}
function calculateTotal() {
var total = getCupCakeSizePrice() * getToppingPrice() + getSeperatlyPrice() + getInscriptionPrice();
var totalEl = document.getElementById('totalPrice');
document.getElementById('totalPrice').innerHTML = "Your Total is: $" + total;
totalEl.style.display = 'block';
}
function hideTotal() {
var totalEl = document.getElementById('totalPrice');
totalEl.style.display = 'none';
}
- Less important issue is that my total should be rounded to a two decimal number. Could anybody point me to Math .js example that’s clear on how this works?
Any other remarks are always welcome. I’m Learning & can take criticism!! :