HashTable

雜湊表

by Chris_Walter

JavaScript

class HashTable{
  constructor(){
	  this.table = [];
	}
	hash(key){
	  let sum = 0;
		for(let i=0; i<key.length; i++){
		  sum+= key.charCodeAt(i);
		}
		return sum % 37;
	}
	put(key, value){
	  let position = this.hash(key);
		console.log(`雜湊值: ${position}`);
	  this.table[position] = value;
	}
	get(key){
	  return this.table[this.hash(key)];
	}
	remove(key){
	let position = this.hash(key);
	  this.table[position] = undefined;
	}
}

let hashtable1 = new HashTable();
hashtable1.put('Chocolate', 100);
console.log(hashtable1.get('Chocolate'));