Forum Moderators: open

Message Too Old, No Replies

Looping on an Alert

Debugging - one Alert seen on a 17-count loop

         

bsmither

6:52 pm on Aug 3, 2006 (gmt 0)

10+ Year Member



For lack of a Javascript IDE, I'm throwing alerts all over the place to track program execution.

function allcalc(){
for (row = 0; row < 17; row++){
rowcalc(row);
alert('Row '+row+' is calcd.');
}
}

function rowcalc(row){
eval('document.frm.tbox'+row+'.value = document.frm.chrow'+row+'.checked*arrFees[row][intAttend];');
calc();
if (eval('document.frm.chrow'+row+'.checked')){
alert('Row '+row+' is checked.');
}
}

I've stripped what I believe to be irrelevant code leaving the above.

A set of radio buttons selects a classification (intAttend) and each of these form elements has onClick="allcalc();". A set of checkboxes selects one or many line items (row) and each of these form elements has onClick="rowcalc(row);". Changing the classification should recalculate all rows, changing any row should recalculate just that row. The function calc() sums the values in the tbox's.

I get the rowcalc() alert when a row checkbox goes from off to on.

However, I only get one alert from the allcalc() function. Shouldn't I get 17 alerts from that function (in addition to the pertinent number of alerts from the rowcalc() function)? Is there any condition that would prevent the allcalc() function from completing the iterations?

Fotiman

8:59 pm on Aug 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



So it's failing when it calls rowcalc(1). Let's walk it through:

eval('document.frm.tbox'+row+'.value = document.frm.chrow'+row+'.checked*arrFees[row][intAttend];');

That would be:

eval('document.frm.tbox1.value = document.frm.chrow1.checked*arrFees[row][intAttend];');

Which would evaluate as if you had this on a line of it's own:

document.frm.tbox1.value = document.frm.chrow1.checked*arrFees[row][intAttend];

What is this supposed to be doing? arrFees is not defined. Neither is row or intAttend. Thus, you're probably ending up with a statement like:

document.frm.tbox1.value = false*null;

That would certainly cause a crash. And why are you multiplying a "checked" value (which should be a boolean, true or false)?

I think that's the cause of your problem. Frankly, I'm surprised it even completes successfully 1 time!

[edited by: Fotiman at 8:59 pm (utc) on Aug. 3, 2006]

bsmither

11:30 pm on Aug 3, 2006 (gmt 0)

10+ Year Member



As I mentioned, I stripped all irrelevant code from the sample I posted. Assume (suspend your disbelief) that the array is populated and that intAttend is defined and holds a value (the value from a set of radio buttons). Row is defined - it's the function's incoming named parameter.

So, the evaluted line would be:
document.frm.tbox1.value = document.frm.chrow1.checked*arrFees[1][2];

Are you saying the 'row' as used in the array index is not the same as the 'row' as used in constructing the tbox# name? That's weird.

And, yes, the .checked method return a boolean value which can evaluate, mathematically, to 1 or 0.

Actually, the rowcalc() function works to my satisfaction. It's the allcalc() function that seems to iterate only once.

Fotiman

2:06 pm on Aug 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




Row is defined - it's the function's incoming named parameter.

So, the evaluted line would be:
document.frm.tbox1.value = document.frm.chrow1.checked*arrFees[2];

Umm... no. Your eval statement is incorrect if that's what you're going for.


eval('document.frm.tbox'+row+'.value = document.frm.chrow'+row+'.checked*arrFees[row][intAttend];');

should be:


eval('document.frm.tbox'+row+'.value = document.frm.chrow'+row+'.checked*arrFees['+row+']['+intAttend+'];');

Note, intAttend has to be a global variable in order for this to work. That is, you're not passing it into the function, so it has to be global if you're expecting to read a value from it.

[1][edited by: Fotiman at 2:09 pm (utc) on Aug. 4, 2006]

Fotiman

2:12 pm on Aug 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




And, yes, the .checked method return a boolean value which can evaluate, mathematically, to 1 or 0.

From a debugging point of view, though, this can be confusing. It's not a good habit to get into.

mehh

2:29 pm on Aug 4, 2006 (gmt 0)

10+ Year Member



I think ive found the answer. Try this:

function allcalc(){
for (row = 0; row < 17; row++){
rowcalc(row);
alert('Row '+row+' is calcd.');
}
}

function rowcalc(row){eval('document.frm.tbox'+row+'.value = (document.frm.chrow'+row+'.checked)?arrFees['+row+']['+intAttend+']:0;')
calc();
if (eval('document.frm.chrow'+row+'.checked')){
alert('Row '+row+' is checked.');
}
}
I think the problem was it wasen't reconiseing the boolean as a number and it could have been the arrFees[row] as well allthough that was unlikely

Fotiman

4:32 pm on Aug 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



mehh, nice work. That is a much cleaner implementation (using the boolean value to determine whether or not to return 0 or the value in the array).

bsmither

7:33 pm on Aug 4, 2006 (gmt 0)

10+ Year Member



I'll take mehh's mod to use the short if/then form. That allows me to send a null string to tbox1.

Having added a plethora of alert statements, I can list the steps I see in code execution:

function allcalc(){
alert('Row 0 is being calcd.');
rowcalc(0);
alert('Row 0 was calcd.');
alert('Row 1 is being calcd.');
rowcalc(1);
alert('Row 1 was calcd.');
alert('Row 2 is being calcd.');
rowcalc(2);
alert('Row 2 is calcd.');
}

When I click the radio button that triggers the allcalc() function, I get:
Row 0 is being calcd.
Row 0 is checked. ->(If it is. From rowcalc.)

But I do NOT get any following alerts:
Row 0 was calcd.
...etc.

So, while the actions in rowcalc() do not seem to be a problem, once finished with rowcalc(), I seem to NOT be returning to allcalc().

So, here's a pertinent question: If I try to change a property of a non-existent object (don't ask why), will that stop all Javascript execution? To test, let's say I don't actually have a chrow1 form element. I'll see the error in the Javascript console. Would this error kill the entire onClick() event? My initial presumption is, yes.

Comment: I should have said the .checked 'property' returns a boolean value. The only problem I have with returned boolean values is that 'true' sometimes is 1 and sometimes is -1, depending on the language. Now, if differing browsers implement their javascript interpreters differently, that will be a problem. However, I typically wrap a Bool*Val expression in abs(). In the days of .001 GHz machines, any code that was quicker was a good thing.

bsmither

9:06 pm on Aug 4, 2006 (gmt 0)

10+ Year Member



Yes, that was the problem. Referencing a non-existent object stopped the interpreter from executing the next statement. So I added a bunch of if(that){that.do;} statements.

But everything else works just the way I coded it.

'Preciate the replies.