Forum Moderators: open
I have a lot of statements like this:
if(!(document.q.q202[0].checked¦¦document.q.q202[1].checked¦¦document.q.q202[2].checked¦¦document.q.q202[3].checked¦¦document.q.q202[4].checked)){...}
And I was thinking I could cut it back to something like this:
if(!document.q.q202[0¦¦1¦¦2¦¦3¦¦4].checked){...}
But it doesnt seem to work. Does anybody know of any other ways I could but back my highly redundant statements?
Thanks
/*
Start a for loop [developer.mozilla.org]. This is the overall structure you see:for ( ... ) { ... }
Notice it has two parts:
- The first part (between the parentheses) has three parts:
a) An initial value for the counter [ var i=0; ]. We use this to start counting at zero, since the first index of an array is zero.
Note that 'i' is commonly used as a counting variable, but not for any particular reason. Feel free to use 'fubar' or anything else instead...
b) A condition or test [ i<document.q.q202.length ]. This is the condition that determines whether the loop will execute--in this case, if 'i' is less than the length [developer.mozilla.org]
of the given array, the loop will execute again...c) An increment [developer.mozilla.org] [ i++ ]. We use this to increase the value of the variable 'i'--this makes sure that the loop will stop at some point, because if we keep adding one to i, eventually the value of i will be greater than the number of indices in the array...
- The second part {between the braces} is the statement part. Here, you put one or more statments that will be executed every time the loop loops.
In this particular case, every loop of will check to see if document.q.q202[i] is not checked, and do something--or not--depending on whether the expression (!document.q.q202[1].checked) evaluates as true or false.
*/
for (var i=0;i<document.q.q202.length; i++) {
if (!document.q.q202[i].checked) { ... }
}
-b
does anybody know if its more CPU demanding?
I guess it should take about twice as much CPU time as the original. Before I suggest why (and a solution) I'll say that it probably doesn't really matter so much in this type of context. The user clicks "Submit"; something happens. They won't notice if it takes 20 ms or 80. That is unless your are repeating this kind of coding over a gargantuan form and / or you have demanding Flash animations being CPU gluttons on the page.
for (var i=0;i<document.q.q202.length; i++) {
____if (!document.q.q202[i].checked) { ... }
}
Here's some of the things happening on every iteration:
Referencing the document.q.q202 collection:
This is all costly. Referencing properties of DOM objects is probably the more time consuming part of the scripting, far more than "pure" Javascript things like comparing two values, or incrementing a variable.
..and in the loop,
document.q.q202 is referenced twice. This is why I reckon it will take about twice as long. The DOM object properties:
length and checked are also referenced on each iteration. The usual approach is to cache as much (and as far down each reference chain) as possible in local variables before entering a loop. In many cases, this can also make the code shorter, and easier to manage too.
In this case, we cache document.q.q202 and it's length.
[pre]
var q202 = document.q.q202;
for (var i=0,L=q202.length; i<L; i++) {
if (!q202[i].checked) { ... }
} [/pre]
[pre]
var q202 = document.q.q202, i = q202.length;while (i>0;) {
if (!q202[--i].checked) { ... }
} [/pre]