JSFiddle - React, Tailwind, and code Playground

by hesster92

HTML

Password Hashing
<br><br> Password to Store
<br>
<input type="text" id="Password" value="">
<br>
<input type="button" value="Submit" id="add">
<br> Check for Password
<br>
<input type="text" id="pwdCheck" value="">
<br>
<input type="button" value="Submit" id="check">


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

JavaScript

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

function Node(_content) {
  this.content = _content;
  this.next = null;
}
List.prototype.push = function(_content) {
  var node = new Node(_content);

  if (!this.head) {
    this.head = node;
    this.length = 1;
  } else {
    var current = this.head;
    while (current.next) {
      current = current.next;
    }
    current.next = node;
    this.length++;
  }
}
List.prototype.pop = function() {
  if (this.head) {
    var getsPopped = this.head;
    this.head = this.head.next;
    this.length--;
    return getsPopped.content;
  } else {
    return false;
  }
}
List.prototype.search = function(_item) {
  let current = this.head;
  while (current) {
    if (current.content == _item) {
      document.getElementById('output').innerHTML = "<br>Password found";
      return true;
    } else {
      document.getElementById('output').innerHTML = "<br>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 * 32) - hash) + char;
    hash = hash & hash;
  }
  return hash;
}

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


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