/    Sign up×
Community /Pin to ProfileBookmark

I need help figuring out some code if any of you could explain this to me

I have dabbled in coding for a while now and have justs recently started teaching myself JavaScript. I am using a few sources to learn from, and one of them had me program a hanoi tower game. Everything is working according to plan, I just cant figure out how its working. Why is it turning out the way it is. If someone could explain this to me it would be great.

Thanks in advance,
Danny Bussell

here is the code…

var hanoi = function hanoi(disc, src, aux, dst)
{
if (disc > 0)
{
hanoi(disc – 1, src, dst, aux);
document.writeln(‘Move disc ‘ +
disc + ‘ from ‘ + src + ‘ to ‘ + dst);
hanoi(disc – 1, aux, src, dst);
}
};
hanoi(3, ‘Src’, ‘Aux’, ‘Dst’);

–and here is the output…
Move disc 1 from Src to Dst
Move disc 2 from Src to Aux
Move disc 1 from Dst to Aux
Move disc 3 from Src to Dst
Move disc 1 from Aux to Src
Move disc 2 from Aux to Dst
Move disc 1 from Src to Dst

Whats confusing me:
When the function is called, it is setting the ‘disc’ to 3. The first thing being done after it realizes 3>0 is to subtract 1 from ‘disc’. Directly after that, it writes the line. So I dont get how the first line of the output is showing “disc 1” is moved instead of “disc 2” (3-1=2 is my logic). Anther thing, (you cant see it here for lack of tabs) the first line of output “Move disc 1…” is tabbed over 2 or 3 times…dont know why. Its not really my main concern here, but it baffles me non-the-less.

I have really been trying to understand the reasoning behind the output of code and am not wanting to go further learning till I can understand and apply this correctly.

Thanks to whoever answers this, I have a few more questions on this chunk of coding, but really wanted to get this one answered. So if someone knowledged decides to take a whack at answering this one, I would not mind a more detailed explanation if you wish.

Thanks again

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@thraddashJan 18.2011 — The wonders of recursion.

Check this code out and you will see at what stage output happens...

[CODE]var hanoi = function hanoi(disc, src, aux, dst)
{
if (disc > 0) {
document.writeln(' ---- ' + disc);
hanoi(disc - 1, src, dst, aux);
document.writeln('<b>Move disc ' + disc + ' from ' + src + ' to ' + dst + '</b><br />');
hanoi(disc - 1, aux, src, dst);
}
document.writeln('&lt; END<br />');

};
hanoi(3, 'Src', 'Aux', 'Dst');[/CODE]



Output
[CODE]---- 3 ---- 2 ---- 1 < END
Move disc 1 from Src to Dst
< END
< END
Move disc 2 from Src to Aux
---- 1 < END
Move disc 1 from Dst to Aux
< END
< END
< END
Move disc 3 from Src to Dst
---- 2 ---- 1 < END
Move disc 1 from Aux to Src
< END
< END
Move disc 2 from Aux to Dst
---- 1 < END
Move disc 1 from Src to Dst
< END
< END
< END
< END[/CODE]
Copy linkTweet thisAlerts:
@rpg2009Jan 20.2011 — thraddash,

You seem to have a good understanding of recursion so any recommendations for learning it (books, links etc)?

I have a basic understanding (the heap/stack, the base condition etc), but would love to get to that point where it all clicks nicely into place.

dannybussell,

Is that from Javascript the Good Parts courtesy of Douglas Crockford? If you are new to recursion (maybe not), it might be better to start with a simple example like a factorial

[CODE]function factorial(n){
if (n==1) {
return 1;
} else {
return (n * factorial(n-1));
}
}

factorial(5); // 5*4*3*2*1 = 120[/CODE]


Oh and google 'recursion' and it comes up with 'did you mean [I][U]recursion[/U][/I]'. How geeky is that?
Copy linkTweet thisAlerts:
@thraddashJan 20.2011 — Resursion is a really simple concept.

I normally use it when going through a hierarchy of data, such as parented arrays. Or a better example is to recurs through elements in an HTML document.

They could have written the factor code like
[CODE]function factorial(n)
{
return n === 1 ? 1 : n * factorial(n - 1);
}[/CODE]


I saw that in Google too, I thought I couldn't spell for a while ?
Copy linkTweet thisAlerts:
@dannybussellauthorJan 20.2011 — That code [I]is[/I] from JavaScript the Good Parts, and I actually found that example you posted and have been trying to use that to gain a better understanding. Though I am sort of getting it, im still trying to wrap my mind around it. I dont feel confident I could apply recursion into further code yet.

I see how it works with the factorial() function. Though I am still trying to figure out the hanio puzzle. I still dont get how the order of the disks in the output is being generated 1,2,1,3,1,2,1. Any explanation?

Thanks for the help by the way.
×

Success!

Help @dannybussell spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...