changed the function

changed the function
This commit is contained in:
Christian Bender
2017-12-26 19:23:01 +01:00
committed by GitHub
parent 152be8fe6c
commit 80b201bc58

View File

@ -1,19 +1,19 @@
### sdbmHashAlgorithm ### sdbmHashAlgorithm
This algorithm is a simple hash-algorithm that hashes it's input string `s` into a whole number. This algorithm is a simple hash-algorithm that hashs it input string 's' into a whole number.
The function iterates over each character in string `s` and updates the `hashCode` in each iteration.
``` js ``` js
const sdbm = s => { const sdbm = str => {
let hashCode = 0; let arr = str.split('');
for (let i = 0; i < s.length; i++) { return arr.reduce( (hashCode,currentVal) =>{
hashCode = s.charCodeAt(i) + (hashCode << 6) + (hashCode << 16) - hashCode; return hashCode = currentVal.charCodeAt(0) + (hashCode << 6) + (hashCode << 16) - hashCode;
} }
return hashCode; ,0)
} }
/*
console.log(sdbm("name")) // -3521204949 // examples
console.log(sdbm("age")) // 808122783 console.log(sdbm("name")) // -3521204949
*/ console.log(sdbm("age")) // 808122783
``` ```