Assignment 10
by pat spag
HTML
<div id="output">
</div>
<div id="output2">
</div>
JavaScript
var originalList = new list();
var indexes = [];
var listLength = 5;
var char;
function node (content_A, content_B){
this.contentA = content_A;
this.contentB = content_B;
this.next = null;
this.prev = null;
return this; //doublke chcke
}
function list(){
this.head = null;
this.tail = null;
this.length = 0;
this.push = function(content_A, content_B){
if (this.head === null){
this.head = new node(content_A, content_B);
this.tail = this.head;
this.length = 1;
return this;
} else {
var addedNode = new node(content_A, content_B);
addedNode.prev = this.tail;
this.tail.next = addedNode;
this.tail = addedNode;
this.length+=1;
return this;
}
}
this.toString = function(){
var str="";
var displayedNode = this.head;
if (this.head === null){ //avoiding type coersion
str = "";
}
while (displayedNode !== null) {
str += displayedNode.contentA + " ";
displayedNode = displayedNode.next;
}
return str;
}
this.hash = function(){
var hashedNode = this.head;
while (hashedNode !== null){
hashedNode = sdbmCode(hashedNode.contentA);
hashedNode.source = hashedNode.contentA;
hashedNode = hashedNode.next;
}
document.getElementById("output2").innerHTML += this.toString();
}
}
//
var sdbmCode = function(str){
var hash = 0;
for (i = 0; i < str.length; i++) {
char = str.charCodeAt(i);
hash = char + (hash << 6) + (hash << 16) - hash;
}
return hash;
}
function populateList(){
var test = new list();
for (var i = 0; i < listLength; i +=1 ){
//re think about listNum
var listNum = Math.floor(Math.random() * 45);
test.push(listNum);
}
document.getElementById("output").innerHTML = "Random <br> " + test.toString();
}
populateList();