Giveny
Giveny

Reputation: 69

Why doesn’t my Javascript price calculator work?

I can't get the below script to work and I really can't see why. Should work fine

Every variable is accounted for, however there is no price showing up?

The HTML is viewable on this page: http://virkfilm.dk/prisb/

 var cake_prices = new Array();
 cake_prices["filmtype1"]=5000;
 cake_prices["filmtype2"]=5000;
 cake_prices["filmtype3"]=5000;


 var filling_prices= new Array();
 filling_prices["Canon"]=0;
 filling_prices["Blackmagic"]=2000;
 filling_prices["Red"]=5000;

  var rejse_prices= new Array();
 rejse_prices["storkbh"]=0;
 rejse_prices["sjaelland"]=300;
 rejse_prices["fyn"]=600;
 rejse_prices["jylland"]=1000;

  var dage_prices= new Array();
 dage_prices["1"]=0;
 dage_prices["2"]=3500;
 dage_prices["3"]=7000;


  var speak_prices= new Array();
 speak_prices["nospeak"]=0;
 speak_prices["dansk"]=1600;
 speak_prices["engelsk"]=1600;



function getCakeSizePrice()
{  
    var cakeSizePrice=0;
    var theForm = document.forms["prisberegner"];
    var selectedCake = theForm.elements["selectedcake"];
    for(var i = 0; i < selectedCake.length; i++)
    {
        if(selectedCake[i].checked)
        {
            cakeSizePrice = cake_prices[selectedCake[i].value];
            break;
        }
    }
    return cakeSizePrice;
}


function getFillingPrice()
{
    var cakeFillingPrice=0;
    var theForm = document.forms["prisberegner"];
     var selectedFilling = theForm.elements["filling"];


    cakeFillingPrice = filling_prices[selectedFilling.value];

    return cakeFillingPrice;
}

function getRejsePrice()
{
    var RejsePrice=0;
    var theForm = document.forms["prisberegner"];
     var selectedRejse = theForm.elements["rejse"];


    RejsePrice = rejse_prices[selectedRejse.value];

    return RejsePrice;
}

function getDagePrice()
{
    var DagePrice=0;
    var theForm = document.forms["prisberegner"];
     var selectedDage = theForm.elements["dage"];


    DagePrice = dage_prices[selectedDage.value];

    return DagePrice;
}

function getSpeakPrice()
{
    var SpeakPrice=0;
    var theForm = document.forms["prisberegner"];
     var selectedDage = theForm.elements["speak"];


    SpeakPrice = speak_prices[selectedSpeak.value];

    return SpeakPrice;
}


function lydPrice()
{
    var lydPrice=0;
    var theForm = document.forms["prisberegner"];
    var lyd = theForm.elements["lyd"];
    if(lyd.checked==true)
    {
        lydPrice=1500;
    }
    return lydPrice;
}

function speakoverPrice()
{
    var speakoverPrice=0;
    var theForm = document.forms["prisberegner"];
    var speakover = theForm.elements["speakover"];
    if(speakover.checked==true)
    {
        speakoverPrice=1600;
    }
    return speakoverPrice;
}

function uspeakPrice()
{
    var uspeakPrice=0;
    var theForm = document.forms["prisberegner"];
    var uspeak = theForm.elements["uspeak"];
    if(uspeak.checked==true)
    {
        uspeakPrice=1000;
    }
    return uspeakPrice;
}

function musikPrice()
{

    var musikPrice=0;
    var theForm = document.forms["prisberegner"];
    var musik = theForm.elements["musik"];
    if(musik.checked==true){
        musikPrice=1200;
    }
    return musikPrice;
}


function storyPrice()
{

    var musikPrice=0;
    var theForm = document.forms["prisberegner"];
    var story = theForm.elements["story"];
    if(story.checked==true){
        storyPrice=1000;
    }
    return storyPrice;
}

function includecandlesPrice()
{

    var includecandlesPrice=0;
    var theForm = document.forms["prisberegner"];
    var includecandles = theForm.elements["includecandles"];
    if(includecandles.checked==true){
        includecandlesPrice=1000;
    }
    return includecandlesPrice;
}        

function calculateTotal()
{
    var cakePrice = getCakeSizePrice() + getSpeakPrice() + getFillingPrice() + getRejsePrice() + getDagPrice() + lydPrice() + speakoverPrice() + uspeakPrice() + musikPrice() + storyPrice() + includecandlesPrice();

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

}

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

I should thank javascript-coder.com for the script template that I've tried to adapt.

Upvotes: 0

Views: 150

Answers (2)

skewl84
skewl84

Reputation: 184

SpeakPrice = speak_prices[selectedSpeak.value];

The selectedSpeak variable is not defined. Define it as per your requirement.

Upvotes: 1

Flight Odyssey
Flight Odyssey

Reputation: 2287

Where you have

var selectedDage = theForm.elements["speak"];

it should be

var selectedSpeak = theForm.elements["speak"];

Otherwise selectedSpeak is not defined anywhere.

Upvotes: 1

Related Questions