Assignment 10 - Hashing Function

by aren_anderson

HTML

<h1>
Hashing Function
</h1>

<h4>
Please enter a password: <input type="textbox" id="tbPass" />
</h4>

<input type="button" id="btnAdd"  value="Submit" onclick="add();"/>

<h4>
Check for stored password: <input type="textbox" id="tbCheck" />
</h4>

<input type="button" id="btnCheck"  value="Submit" onclick="check();"/>

<br/><br/>
<div id="output">
</div>

JavaScript

debugger;
var list = new LinkedList();

//linked list
function LinkedList() {
	this.length = 0;
  this.head = null;
  this.length = null
}

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

//function to add content to Node
LinkedList.prototype.add = function(content){
	var node = new Node(); 
  node.content = content;
  
  if (this.head == null){
  	this.head = node; 
    this.length = 1;
    return node;
  }

  if (this.tail == null){
  	this.tail = node;
    this.tail.prev = this.head;
    this.head.next = this.tail;
    this.length++;
    return node;
  }
  
  this.tail.next = node;
  node.prev = this.tail;
  this.tail = node;
  this.length++;
  return node;
}

//check if password is found in hashtable
LinkedList.prototype.check = function(content){
	var item = this.head;
  while (item){
  	if (item.content == content){
    	document.getElementById("output").innerHTML = "Password found";
      return true;
  }else{
  	document.getElementById("output").innerHTML = "Password not found";
    return false;
  }
  item = item.next;
  }
}

function add(){
	var input = document.getElementById("tbPass").value;
  list.add(hashCode(input));
}

function check(){
	var checked = document.getElementById("tbCheck").value;
  list.check(hashCode(checked));
}

function hashCode(str){
	var hash = 0;
  for (var i = 0; i < str.length; i++){
  	var char = str.charCodeAt(i);
    hash = ((hash << 5) - hash) + char;
    hash = hash & hash;
  }
  return hash;
}