xxxxxxxxxx
25
/* Background:
* https://en.wikipedia.org/wiki/Tower_of_Hanoi
*
* In case you're not familiar with JavaScript's conditional (ternary)
* operator, it's used below as a shortcut for if-else syntax.
* You can read about it here:
*
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_operator
*/
/*
Parameters:
n: number of rings (1, 2, 3, ...)
Return value:
Required number of moves to solve Tower of Hanoi puzzle
*/
function T(n) {
return n === 1 ? 1 : 2 * T(n - 1) + 1;
}
// test
for (let n = 1; n < 6; n++) {
console.log(T(n));
}