Forum Moderators: open

Message Too Old, No Replies

I am missing something I should really know by now!

         

SaminOz

1:00 pm on Jul 16, 2009 (gmt 0)

10+ Year Member



I have listed a function below. The problem variable is writeTotal. It returns NaN or undefined and won't behave itself and help me add up the total order. I'm sure JS is smart enough to work out it's datatype and if I remove it all works (except I get single values, not a total) - can anyone help me understand why js doesn't like my variable writeTotal? The same logic works fine for the strings collected in writeDesc.

Thanks -

Code:

function writeChange_onclick(checkBoxSelected) {

switch (checkBoxSelected) {
case 'DVD-ROM':
var writePrice = 59;
break;
case 'CD-ROM':
var writePrice = 29;
break;
default:
var writePrice = 129;
break;
}
var writeTotal;
writeTotal = writeTotal+ '$' +writePrice; //**this is where I have a problem
var txtBox = document.form1.txtFeedBack;
var priceBox = document.form1.priceFeedBack;
var writeDesc = txtBox.value; //name value of checkbox - but this actually just seems to declare variable
var priceWrite = priceBox.value;
writeDesc = writeDesc + checkBoxSelected; //add the name value of the check box each time
writeDesc = writeDesc + ' $'+writePrice+ '\n'; //add price based on switch statement along with return
txtBox.value = writeDesc; //write value back to textbox
priceBox.value = priceWrite + writeTotal; //write price total back to (price) textbox

//end of function
}

daveVk

1:56 pm on Jul 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



var writeTotal; // declare writeTotal with undefined value
writeTotal = writeTotal+ '$' +writePrice; // = undefined + '$' + writePrice;

Should it just be

var writeTotal = '$' + writePrice; // make text string with $ prefix

Or are you trying to calculate a total somehow ?

SaminOz

10:25 pm on Jul 16, 2009 (gmt 0)

10+ Year Member



I am trying to keep track of a total value. I cannot set var writeTotal = 0; when I declare as this will reset.

My problem must be simple as var1 = var1 + var2; is a pretty common way to go about getting a total isn't it?

daveVk

11:54 pm on Jul 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The "+" operater has 2 uses as in 1+1 (2) and 'foo' + 'bar' (foobar), by including '$' or any text the second meaning in assumed.

The second problem is scope, I assume you mean writeTotal to be added to each time the function is called, so writeTotal needs to exist between calls, that is to be a global variable.

var writeTotal = 0;
function writeChange_onclick(checkBoxSelected) {
...
writeTotal = writeTotal + writePrice; // or writeTotal += writePrice;
...
}

You will also need to consider what happens if item unselected.

SaminOz

5:54 am on Jul 17, 2009 (gmt 0)

10+ Year Member



That's got things working nicely, now on with the rest...

I never really understood why even without the '$' string e.g. two numbers - the variable writeTotal fed back as undefined? I thought js was smart about variables?

var writeTotal = writeTotal + writePrice;

would to me be set as numerical because of writePrice or perhaps there is someway to declare writeTotal - I tried parseInt() but was not greeted with success. Anyway - many thanks again...

Sam.