<html>
<head>
</head>
<body>
<h2>
Juan Alban Franco <br>
Towers of Hanoi
</h2>
<br>
Please select a value <strong> less than 8</strong>
<input type = textbox id = input valu = 7>
<br>
<br>
<input type = button id = calculate value = "Calculate number of steps needed" onclick = Call_Hanoi() >
<span id = "comp" ></span>
<p id = steps>
</p>
<div id = out></div>
</body>
</html>
JavaScript
var string = '';
var counter = 0;
var hanoi = function(disc,source,right_tower,middle_tower) {
if (disc > 0) {
hanoi(disc - 1,source,middle_tower,right_tower);
//string += ("Move disc " + disc + " from " + source + " to " + middle_tower + "<br />");
counter ++;
hanoi(disc - 1,right_tower,source,middle_tower);
};
};
function Call_Hanoi(){
var num = document.getElementById("input").value;
hanoi(num,"Peg 1","Peg 2","Peg 3");
document.getElementById("out").innerHTML = string;
document.getElementById("steps").innerHTML = "Steps Needed "+ counter
document.getElementById("comp").innerHTML = " Time Complexity for Tower of Hanoi Solution = O(2^n)"
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.