betterHash output problems

betterHash output problems
0

#1

Hi, I study some algorihtms in JS. But, i could not understand why am I see nine argument if names at ouput. I see only eight names. What is the problem in my code ?

function HashTable(){
	this.table = new Array(137);
	this.simpleHash = simpleHash;
	this.betterHash = betterHash;
	this.showDistro = showDistro;
	this.put = put;
}

function put(data){
	var pos = this.betterHash(data);
	this.table[pos] = data;
}

function simpleHash(data){
	var total = 0;
	for(var i = 0; i < data.length; i++){
		total += data.charCodeAt(i);
	}
	console.log("Hash value: " + data + " -> " + total);
	return total % this.table.length;
}

function showDistro(){
	//var n = 0;
	for(var i =0; i < this.table.length; i++){
		if(this.table[i] != undefined){
			console.log(i + ": " +this.table[i]);
		}
	}
}

function betterHash(string){
	const H = 37;
	var total = 0;
	for (var i = 0; i < string.length; i++){
		total += H * total + string.charCodeAt(i);
	}
	total = total % this.table.length;
	if(total < 0){
		total += this.table.length - 1;
	}
	return parseInt(total);
}

var someNames = ["David", "Jennifer", "Donnie", "Raymond", "Cynthia", "Mike", "Clayton", "Danny", "Jonathan"];
var hTable = new HashTable();
for(var i = 0; i < someNames.length; i++){
	hTable.put(someNames[i]);
}
hTable.showDistro();

#2

Hi, is it a freeCodeCamp challenge?

Anyway, it’s normal that you see only eight name since your hashing function gives the same result for "David" and "Jonathan" which is 80.
You have here what we call a hash collision.