Assignment 7

Recursions

by Jenni Meiklejohn

HTML

<center> <h2><div id="t">Assignment 7</div></h2> </center>
<h3><center> Recursions</center></h3>
<h4><center>The complexity of the Tower of Hanoi is 2^(n - 1) and 2(-2^n)  </center></h4> Should we be concerned with concerned with the legend of the world ending when the 64 disk solution is physically solved it it takes 2 seconds for each move? No
<br><br>

<input type="text" id="numOfDisks" value="3" size="5">
<input type="button" value="Start" onclick="numberOfMoves()">
<div id="results">
  <br>
</div>

JavaScript

var Node = function(_content) {
  this.next = null;
  this.content = _content;
}

var Chain = function() {
  this.head = null;
  this.tail = null;

  this.push = function(_content) {
    if (this.tail == null) {
      this.tail = new Node(_content);
      this.head = this.tail;
      return this;
    }

    var addedNode = new Node(_content);
    addedNode.last = this.head;
    this.head.next = addedNode;
    this.head = addedNode;
    return this;
  }
}

function TowerofHanoi(disk) {
  if (disk > 0) {
    TowerofHanoi(disk - 1);
    counter++;
    TowerofHanoi(disk - 1);
  }
}

function numberOfMoves() {
  var disks = parseInt(document.getElementById("numOfDisks").value);
  fillStack();
  if (disks) {
    TowerofHanoi(disks);
    document.getElementById("results").innerHTML = "</br> It took " + counter + " moves to complete the Tower of Hanoi with " + disks + " disks";
  }
}

function fillStack() {
  var disks = document.getElementById("numOfDisks").value;
  for (i = disks; i > 0; i--) {}
}


var stackOne = new Chain();
var stackTwo = new Chain();
var stackThree = new Chain();
var counter = 0;