const MAX_SIZE = 5 let a = [5, 4, 3, 2, 1], b = [], c = []; let abc = { a, b, c } function repeat_str(s, n) { return Array(n).fill(s).join('') } function hnt(keys, n) { let key0 = keys[0], key1 = keys[1], key2 = keys[2]; if (n === 1) { let tip = `from ${key0} to ${key2}` let k = abc[key0].pop() abc[key2].push(k) console.log(`${tip} moving ${k}`) console.log(`after moving: a:[${abc.a}] b:[${abc.b}] c:[${abc.c}]`) } else { console.log(repeat_str('|', MAX_SIZE - n) + repeat_str('-', 20)) let move1 = `${keys[0]}${keys[2]}${keys[1]}`; console.log(`hnt(${move1},${n-1})`) hnt(move1, n - 1) let move2 = `${keys[0]}${keys[1]}${keys[2]}`; console.log(`hnt(${move2},1)`) hnt(move2, 1) let move3 = `${keys[1]}${keys[0]}${keys[2]}`; console.log(`hnt(${move3},${n-1})`) hnt(move3, n - 1) console.log(repeat_str('|', MAX_SIZE - n) + repeat_str('-', 20)) } } hnt("abc", 5)