Passwords

by vzufelt

HTML

<h3>
Assignment 10
</h3>

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

<form id="form">
  <div id="div">
    Add Password<br>
    <input type="text" id="passwordAdd" value=""><br>
    <input type="button" value="Submit" id="add"><br>
    Find Passwords<br>
    <input type="text" id="passwordFind" value="" /><br>
    <input type="button" value="Submit" id="find" />
  </div>
</form>

JavaScript

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

function List() {
  this.head = null;
  this.length = 0;
}

List.prototype.push = function(content) {
  var node = new Node(content);

  if (!this.head) {
    this.head = node;
    this.length++;
  } else {
    var current = this.head;
    while (current.next) {
      current = current.next;
    }
    current.next = node;
    this.length++;
  }
}
List.prototype.pop = function() {
  if (this.head) {
    var itemToPop = this.head;
    this.head = this.head.next;
    this.length--;
    return itemToPop.content;
  } else {
    return false;
  }
}
List.prototype.search = function(item) {
  let current = this.head;
  while (current) {
    if (current.content == item) {
      document.getElementById('output').innerHTML = "Password found";
      return true;
    } else {
      document.getElementById('output').innerHTML = "Password not found";
      return false;
    }
    current = current.next;
  }

}

var list = new List();
function hashCode(str) {
  var hash = 0;

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

document.getElementById('find').onclick = function() {
  var input = document.getElementById("passwordFind").value;
  list.search(hashCode(input));
}

document.getElementById('add').onclick = function() {
  var uInput = document.getElementById("passwordAdd").value;
  list.push(hashCode(uInput));
}