1

Approached the towers of hanoi problem and was wondering why my output starts at "disk 1".

Based on how the code is written to be (taken from geeksforgeeks website), shouldn't the expected output be:

"Move disk 4 from rod A to rod C."

and then once it is called again (n-1) the output should be:

"Move disk 3 from rod B to rod C."

I don't understand how it logs starting from disk 1.

function towersOfHanoi(n,f,t,a) {
            if (n==0){
                return;
            }
            towersOfHanoi(n-1, f, a, t);
                console.log("Move disk "+ n + " from rod " + f +" to rod "+ t +".")
            towersOfHanoi(n-1, a, t, f);
            
        }

        let N = 4;
        towersOfHanoi(N,"A","C","B");
bamb1
  • 69
  • 5
  • 1
    That `console.log()` call is not reached until the recursive sequence from the call *before* that line has gotten down to 1. – Pointy Aug 09 '23 at 02:10
  • 2
    [Please do not upload images of code/data/errors.](//meta.stackoverflow.com/q/285551) Aside from that: it's unclear what you're asking (are you asking for someone to explain your code to you?) Please edit to be specific. – David Makogon Aug 09 '23 at 02:11
  • Whoops. Just deleted! Thanks for the heads up, David. And yes, that makes sense, Pointy. Thank you for the help! – bamb1 Aug 09 '23 at 02:32

0 Answers0