Recursion can be confusing if you haven't dealt with it, but I will try to explain:
You call:f(3)
So it enters and calls: document.write("<p style='background: blue'>" + counter);
which prints then blue line with a 2 as the counter is 2 after the counter--
, then it calls f(2)
(because counter != 0
as it is 2.)
So it calls: document.write("<p style='background: blue'>" + counter);
which prints then blue line with a 1 as the counter is 1 after the counter--
, then it calls f(1)
(because counter != 0
as it is 1.)
So it calls: document.write("<p style='background: blue'>" + counter);
which prints then blue line with a 0 as the counter is 0 after the counter--
, then it does not call f(0)
(because counter == 0
)
so now we complete the function we are in (where 'counter == 0') and it calls: document.write("<p style='background: yellow'>" + counter);
which prints then yellow line with a 0 as the counter is 0
then we go back to the function were we called f(0)
from and counter == 1
and that calls document.write("<p style='background: yellow'>" + counter);
which prints then yellow line with a 1 as the counter is 1
then we go back to the function were we called f(1)
from and counter == 2
and that calls document.write("<p style='background: yellow'>" + counter);
which prints then yellow line with a 2 as the counter is 2
I know this is confusing but I hope it helps?
* EDIT * Here is my more detailed and hopefully very clear explanation!
Here is my try:
function f(3) {
counter--;
document.write("<p style='background: blue'>" + counter);
if(counter != 0) {
f(counter) // counter equals 2
}
document.write("<p style='background: yellow'>" + counter);
}
So lets just replace f(counter)
aka f(2)
above with what is actually being called (placing counter w/ counter2 because they are separate variables:
function f(3) {
counter--;
document.write("<p style='background: blue'>" + counter);
if(counter != 0) {
counter2 = counter;
counter2--;
document.write("<p style='background: blue'>" + counter2);
if(counter2 != 0) {
f(counter2) // counter2 equals 1
}
document.write("<p style='background: yellow'>" + counter2);
}
}
document.write("<p style='background: yellow'>" + counter);
}
Now again, lets just replace f(counter2)
aka f(1)
above with what is actually called (replacing counter2 w/ counter3 as again they are separate variables.)
function f(3) {
counter--;
document.write("<p style='background: blue'>" + counter); // prints 2
if(counter != 0) {
counter2 = counter;
counter2--;
document.write("<p style='background: blue'>" + counter2); // prints 1
if(counter2 != 0) {
counter3 = counter2;
counter3--;
document.write("<p style='background: blue'>" + counter3); // prints 0
if(counter3 != 0) { // counter3 equals 0 so this is false
f(counter3); // this is never called because counter3 DOES equal 0
}
document.write("<p style='background: yellow'>" + counter3); // prints 0
}
document.write("<p style='background: yellow'>" + counter2); // prints 1
}
}
document.write("<p style='background: yellow'>" + counter); // prints 2
}