JSFiddle - React, Tailwind, and code Playground

by leiperte

HTML

Enter a new password: 
<input type="textbox" id="newPassword" /> 
<input type="button" id="store" value="Add New Password" onClick="storePassword()" /> 
<br/> <br/> 
Enter a password to compare: 
<input type="textbox" id="checkPassword" /> 
<input type="button"
  id="compare" value="Compare password" onClick="compare()" /> <br/>
  
<div id="output">

</div>

JavaScript

//Assignment 10

var passwordList = new LinkedList();

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

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

LinkedList.prototype.add = function(value) {
  var node = new Node();
  node.value = value;
  if (!this.head) {
    this.head = node;
    this.length++;
  } else {
    var content = this.head;
    while (content.next) {
      content = content.next;
    }
    content.next = node;
    this.length++;
  }
}

LinkedList.prototype.check = function(value) {
  var content = this.head;
  while (content) {
    if (content.value == value) {
      document.getElementById("output").innerHTML = "Password already exist.";
      return true;
    } else {
      document.getElementById("output").innerHTML = "Password does not exist.";
      return false;
    }
    content = content.next;
  }
}

function storePassword() {
  var pass = document.getElementById("newPassword").value;
  passwordList.add(hashCode(pass));
  alert("password added")
}

function compare() {
  var check = document.getElementById("checkPassword").value;
  passwordList.check(hashCode(check));
}

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;
}