Recursion test

by Joe LeMonnier

HTML

<script src="https://getfirebug.com/firebug-lite-debug.js"></script>

JavaScript

/*var b=0;
var c=0;
var hanoi= function (a){
if (a>0){
document.write('<br>&nbsp c=',c++,'&nbsp b= ', b,'&nbsp a=',a,'first');
 hanoi(a-1)
b++;
document.write('<br>c=',c++,'&nbsp &nbsp b=',b,'&nbsp &nbsp a =',a);
hanoi(a-1);
}

}

hanoi(2);



hanoi(2) passes if   calls
hanoi(1) passes if  calls
hanoi(0) FAILS RETURNS a=1. b=1



*/
var a=0;
var hanoi = function (disc, src, aux, dst) {
    if (disc > 0) {
document.write(a++);
        hanoi(disc - 1, src, dst, aux);
        document.writeln('<br>Move disc ' + disc +
                ' from ' + src + ' to ' + dst);
        hanoi(disc -1, aux, src, dst);
    }
};

hanoi(2,'left','middle','right');

/*
         hanoi(2) passes'if test'
Calls hanoi(1) passes 'if test'
Calls hanoi(0) FAILS   'If test'