JS: Good parts - Ch.4 - Functions

recursion

by Denise Nepraunig

JavaScript

/* 
All texts and infos I have taken from:
'JavaScript: The Good Parts' by Douglas Crockford
*/

// hanoi


var hanoi = function hanoi(disc, src, aux, dst) {
    if(disc > 0) {
        hanoi(disc - 1, src, dst, aux);
        console.log('move disc ', disc, 'from', src, 'to', dst);
        hanoi(disc - 1, aux, src, dst);
    }
};

hanoi(3, 'Src', 'Aux', 'Dst');

var factorial = function factorial(i,a) {
    a = a || 1;
    if (i < 2) {
        return a;
    }
    return factorial(i - 1, a * i);
};

console.log("fac of 4:", factorial(4));