编辑代码

/**
 * 把n-1个盘子由 A 移到 B;
 * 把第n个盘子由 A 移到 C;
 * 把n-1个盘子由 B 移到 C;
 */
function hanluota(n, A, B, C) {
    if (n === 1) {
        // 只有 1 个,直接 A -> C
        move(A, C)
    } else {
        // n - 1,先将 A -> B
        hanluota(n - 1, A, C, B);
        // 将 n 从 A -> C
        move(A, C)
        // 在将 B -> C
        hanluota(n - 1, B, A, C)
    }
}

function move(A, C) {
    console.log(A + '->' + C)
}

hanluota(3, 'A', 'B', 'C')