Indexes
Assignment 10
by austinmillett
HTML
Please create a password:
<input type="textbox" value="" id="passInput" />
<input type="button" value="Create Password" onclick="add_password()" /><br/><br/><br/>
Please verify your password:
<input type="textbox" id="chkPass" value="" />
<input type="button" value="Verify Password" onclick="check_password()" />
<br/>
<div id="output">
</div>
JavaScript
function LinkedList() {
this.head = null;
this.tail = null;
this.length = 0;
}
function Node(item) {
this.next = null;
this.prev = null;
this.item = item;
}
LinkedList.prototype.add = function(_data) {
var node = new Node();
node.item = _data
if (!this.head) {
this.head = node;
this.tail = node;
} else {
node.previous = this.tail;
this.tail.next = node;
this.tail = node;
}
this.numberOfValues++;
}
function simpleHash(str) {
len = str.length;
hash = 0;
for (i = 1; i <= len; i++) {
char = str.charCodeAt((i - 1));
hash += char * Math.pow(31, (len - i));
hash = hash & hash;
}
return hash;
}
var list = new LinkedList();
function add_password() {
var c = document.getElementById("passInput").value;
c = simpleHash(c);
list.add(c);
list.length++;
alert(c);
}
function check_password() {
var check = document.getElementById("chkPass").value;
check = simpleHash(check);
alert(check);
alert(list.length);
var len = list.length;
var node = list.head;
for(var i =0; i < len; i++){
alert(node.item); // How do I make it not undefined
if (check == node.item){
alert("Successful login attempt");
return;
}
else{
node = node.next;
}
}
alert("Error failed login attempt");
}