Purchase Solution

JavaScript: Towers of Hanoi

Not what you're looking for?

Ask Custom Question

In order to attempt to move the disks from peg 1 to peg 3, develop an algorithm that will print the precise sequence of peg-to-peg disk transfers.

Solve the problem with recursion in mind, it immediately becomes tractable. Moving n disks can be viewed in terms of moving only n-1 disks as follows:

a) Move n -1 disks from peg 1 to peg 2, using peg 3 as a temporary holding area.
b) Move the last disk from peg 1 to peg 3.
c) Move n -1 disks from peg 2 to peg 3, using peg 1 as a temporary holding area.

The process ends when the last task involves moving n = 1 disk (i.e., the base case). This task is accomplished simply by moving the disk, without the need for a temporary holding area.

Write a javascript to solve the problem of the Towers of Hanoi. Allow the end user to enter the number of disks in a text field. Use a recursive tower function with four parameters:

a) The number of disks to be moved.
b) The peg on which the disks are initially threaded.
c) The peg to which the stack of disks is to be moved.
d) The peg to be used as a temporary holding area.

Program should display in a <textarea> the precise instructions it will take to move the disks from the starting peg to the destination peg. For example, to move a stack of three disks from peg 1 to peg 3, your program should display the following series of moves:

1 -> 3 (i.e. movement from peg 1 to peg 3.)
1-> 2
3 -> 2
1-> 3
2 -> 1
2 -> 3
1 -> 3

Attachments
Purchase this Solution

Solution Summary

Solution gives two implementations, both of which assume fair input from the user.

Solution Preview

Please find attached two ways of solving this problem - 221959-v1.html and 221959-v2.html, both of which assume fair input from the ...

Purchase this Solution


Free BrainMass Quizzes
C++ Operators

This quiz tests a student's knowledge about C++ operators.

Basic Networking Questions

This quiz consists of some basic networking questions.

Excel Introductory Quiz

This quiz tests your knowledge of basics of MS-Excel.

C# variables and classes

This quiz contains questions about C# classes and variables.

Basic UNIX commands

Use this quiz to check your knowledge of a few common UNIX commands. The quiz covers some of the most essential UNIX commands and their basic usage. If you can pass this quiz then you are clearly on your way to becoming an effective UNIX command line user.