J
J. J. Cale
In IE6 both these functions *seem* to be working.
I don't see much recursion in this group except the
occasional setTimeout problems. Besides the obvious
stack problems that can occur if one recurses too deep
are there other reasons for this that I should know about?
This example returns the position of the leftmost element
of a specific type but it could be any array or any type
of comparison regex or whatever.
Also I'm sure these can be made better. Any help?
Jimbo
#1 loop
getSmallest(document.getElementsByTagName(tagName));
function getSmallest(aArr) {
var small = aArr[0];
for (var i=1; i < aArr.length; i++) {
if(aArr.offsetLeft >= small.offsetLeft)
continue;
else small = aArr;
}
return small;
}
#2 recursion
var a = document.getElementsByTagName(tagName);
getSmallest( a, a.length-1);
function getSmallest(aArr, n) {
if (n < 0) return false;
if(n == aArr.length-1) smallest = n;
smallest = aArr[smallest].offsetLeft <= aArr[n].offsetLeft? smallest: n;
if(getSmallest(aArr,n-1))
return aArr[smallest].offsetLeft;
return aArr[smallest].offsetLeft;
}
I don't see much recursion in this group except the
occasional setTimeout problems. Besides the obvious
stack problems that can occur if one recurses too deep
are there other reasons for this that I should know about?
This example returns the position of the leftmost element
of a specific type but it could be any array or any type
of comparison regex or whatever.
Also I'm sure these can be made better. Any help?
Jimbo
#1 loop
getSmallest(document.getElementsByTagName(tagName));
function getSmallest(aArr) {
var small = aArr[0];
for (var i=1; i < aArr.length; i++) {
if(aArr.offsetLeft >= small.offsetLeft)
continue;
else small = aArr;
}
return small;
}
#2 recursion
var a = document.getElementsByTagName(tagName);
getSmallest( a, a.length-1);
function getSmallest(aArr, n) {
if (n < 0) return false;
if(n == aArr.length-1) smallest = n;
smallest = aArr[smallest].offsetLeft <= aArr[n].offsetLeft? smallest: n;
if(getSmallest(aArr,n-1))
return aArr[smallest].offsetLeft;
return aArr[smallest].offsetLeft;
}