Post by Schubaltz on Apr 20, 2008 22:46:49 GMT -5
Posted this at G101 a year back. Same as any other, it's useful, but I don't endorse it (probably wouldn't even if I wrote this just today ).
Alright, I wouldn't consider this much of a tutorial, whipped it up in a couple minutes, and i'm not as good at explaining things as I used to be, but learn. >_>
-----------
There'll be times when you'll need to burrow deep within an object's hierarchy (just an example). During these times, you can't always use loop after loop. Especially considering you'll never know how many loops you'll need.
var myArray = [
[
[ ["hey ", "there. "], ["what's ", "up? "] ],
[ ["nothing ", "much "], ["and ", "you? "] ]
]
]
for(var i = 0; i < myArray.length; i ++) {
for(var x = 0; x < myArray.length; x ++) {
for(var e = 0; e < myArray[x].length; e ++) {
for(var r = 0; r < myArray[x][e].length; r ++) {
document.write(myArray[x][e][r]);
}
}
}
}
This gets tedious, and mind-numbing. It'd be a lot easier if we could just rig up a function to do all the work for us.
function count(a, b) {
if(a <= b)
return a + count(a + 1, b);
return 0;
}
count(1, 4)
Simple, enough. This will start the count at 1 and end when a is 4 (1 + 2 + 3 + 4). The return statement is key here. In our above function, count(a + 1, b) is constantly returning a number. We reiterate this function 4 times,
1. return a + count(a + 1, b) // 1 + 9
Now it's gotta run the count function again, and since a isn't greater than b, it repeats
2. return a + count(a + 1, b) // 2 + 7
3. eturn a + count(a + 1, b) // 3 + 4
4. return a + count(a + 1, b) // 4 + 0
With each iteration, the function delves feeper and deeper. At it's deepest, we count 4 + 0, which returns in the previous count iteration as 3 + 4, which returns in the iteration before that, 2 + 7, etc.
Now onto something more complex,
function readArray(a, b) {
if(!b)
b = "";
for(var x = 0; x < a.length; x ++) {
if(typeof a[x] == "object") {
b += readArray(a[x], "");
} else {
b += a[x];
}
}
return b;
}
Alright, now we've got a function that'll read through our array. It's pretty simple, really. It loops through all the elements of argument a, and returns b. If a[x] is an object (another array), we reiterate the function and let it delve deeper into the hierarchy. If not, we assume it's just a simple string and we add it to b.
if(!b)
b = "";
This part is crucial (well, it all is). b is always set to "" at the beginning of the function. When a string is found, it is added to b, and b is returned for however many iterations we had.
b += readArray(a[x], "");
Alright, this is where it gets tricky. b is whatever value right now. Since we've hit another array, we'll reiterate the function. This instance of readArray() will return a value based on what all the iterations inside of IT return. Example:
var myArray = [ [ "hey" ] ]
The function will reiterate once. The return value of this iteration will be "hey", which is added to b, which is then returned through the original function call. Think of it this way: you own a business, you tell the managers what to do. They in turn tell the workers what to do. The end result of the workers then gets passed back to the manager and from him/her, back to you.
Alright, I wouldn't consider this much of a tutorial, whipped it up in a couple minutes, and i'm not as good at explaining things as I used to be, but learn. >_>
-----------
There'll be times when you'll need to burrow deep within an object's hierarchy (just an example). During these times, you can't always use loop after loop. Especially considering you'll never know how many loops you'll need.
var myArray = [
[
[ ["hey ", "there. "], ["what's ", "up? "] ],
[ ["nothing ", "much "], ["and ", "you? "] ]
]
]
for(var i = 0; i < myArray.length; i ++) {
for(var x = 0; x < myArray.length; x ++) {
for(var e = 0; e < myArray[x].length; e ++) {
for(var r = 0; r < myArray[x][e].length; r ++) {
document.write(myArray[x][e][r]);
}
}
}
}
This gets tedious, and mind-numbing. It'd be a lot easier if we could just rig up a function to do all the work for us.
function count(a, b) {
if(a <= b)
return a + count(a + 1, b);
return 0;
}
count(1, 4)
Simple, enough. This will start the count at 1 and end when a is 4 (1 + 2 + 3 + 4). The return statement is key here. In our above function, count(a + 1, b) is constantly returning a number. We reiterate this function 4 times,
1. return a + count(a + 1, b) // 1 + 9
Now it's gotta run the count function again, and since a isn't greater than b, it repeats
2. return a + count(a + 1, b) // 2 + 7
3. eturn a + count(a + 1, b) // 3 + 4
4. return a + count(a + 1, b) // 4 + 0
With each iteration, the function delves feeper and deeper. At it's deepest, we count 4 + 0, which returns in the previous count iteration as 3 + 4, which returns in the iteration before that, 2 + 7, etc.
Now onto something more complex,
function readArray(a, b) {
if(!b)
b = "";
for(var x = 0; x < a.length; x ++) {
if(typeof a[x] == "object") {
b += readArray(a[x], "");
} else {
b += a[x];
}
}
return b;
}
Alright, now we've got a function that'll read through our array. It's pretty simple, really. It loops through all the elements of argument a, and returns b. If a[x] is an object (another array), we reiterate the function and let it delve deeper into the hierarchy. If not, we assume it's just a simple string and we add it to b.
if(!b)
b = "";
This part is crucial (well, it all is). b is always set to "" at the beginning of the function. When a string is found, it is added to b, and b is returned for however many iterations we had.
b += readArray(a[x], "");
Alright, this is where it gets tricky. b is whatever value right now. Since we've hit another array, we'll reiterate the function. This instance of readArray() will return a value based on what all the iterations inside of IT return. Example:
var myArray = [ [ "hey" ] ]
The function will reiterate once. The return value of this iteration will be "hey", which is added to b, which is then returned through the original function call. Think of it this way: you own a business, you tell the managers what to do. They in turn tell the workers what to do. The end result of the workers then gets passed back to the manager and from him/her, back to you.