This is an old thread, but I have to thank you for recommending this book. I ordered it and have been hooked! Incredible writing and storytelling. Thanks from this Texan.
function collatz(i) {
console.log(i); // for visual feedback
if (i <= 0) throw new Error('Input must be a positive integer');
if (i === 1) return i;
if (i % 2 === 0) return collatz(i/2);
return collatz((3*i)+1);
}
collatz(42); // or any positive integer