have the following javascript code
// note: declaring i in this loop
for( var i=0; i<args.length; i++ ) {
var elem = args[i];
...
if( elem.attr == 'class' ) {
// note declaring arr and i in this loop
for( var arr=elem.val.split(' '), i=0; i<arr.length; i++ ) {
element.classList.add(arr[classCt]);
}
continue;
}
}
the problem is that i in the second for loop is the same i as declared in the first for loop.
thought that the var construct allowed multiple variables to be declared separated by commas.
when changed i to classCt in second loop, the code worked as expected
You only have one scope there, so there can only be one variable with the same name. You’re correct that var allows multiple variables to be declared separated by commas, but you can’t declare two different variables with the same name in the same scope. You’re just redeclaring a variable that already exists.
Either change it to
classCt, or do what I do and use the variablej(and so on) for nested loop iterators: