From 64662e07e9517a8d8e3f311cc556cb395d37b02c Mon Sep 17 00:00:00 2001 From: Christian Bender Date: Sun, 24 Dec 2017 23:29:13 +0100 Subject: [PATCH 01/10] sdbm-hash algorithm I added the sdbm hash algorithm --- snippets/sdbm_hash.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 snippets/sdbm_hash.md diff --git a/snippets/sdbm_hash.md b/snippets/sdbm_hash.md new file mode 100644 index 000000000..e588ee5ad --- /dev/null +++ b/snippets/sdbm_hash.md @@ -0,0 +1,13 @@ + +This function implements the **sdbm** hash-algorithm. +It uses the charCodeAt-methode and the shift-operator. + +``` js +function sdbm(s) { + var hashCode = new Number(); + for (var i = 0; i < s.length; i++) { + hashCode = s.charCodeAt(i) + (hashCode << 6) + (hashCode << 16) - hashCode; + } + return hashCode; +} +``` \ No newline at end of file From 83594f1d0f232289d55ed7475858d80c3035b108 Mon Sep 17 00:00:00 2001 From: Christian Bender Date: Sun, 24 Dec 2017 23:29:39 +0100 Subject: [PATCH 02/10] Delete sdbm_hash.md --- snippets/sdbm_hash.md | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 snippets/sdbm_hash.md diff --git a/snippets/sdbm_hash.md b/snippets/sdbm_hash.md deleted file mode 100644 index e588ee5ad..000000000 --- a/snippets/sdbm_hash.md +++ /dev/null @@ -1,13 +0,0 @@ - -This function implements the **sdbm** hash-algorithm. -It uses the charCodeAt-methode and the shift-operator. - -``` js -function sdbm(s) { - var hashCode = new Number(); - for (var i = 0; i < s.length; i++) { - hashCode = s.charCodeAt(i) + (hashCode << 6) + (hashCode << 16) - hashCode; - } - return hashCode; -} -``` \ No newline at end of file From 369ecbcd56ad418f8fd91d742eca555405490719 Mon Sep 17 00:00:00 2001 From: Christian Bender Date: Sun, 24 Dec 2017 23:30:22 +0100 Subject: [PATCH 03/10] added sdbm hash algorithm I added the sdbm hash algorithm as a function. --- snippets/sdbm-hash.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 snippets/sdbm-hash.md diff --git a/snippets/sdbm-hash.md b/snippets/sdbm-hash.md new file mode 100644 index 000000000..e588ee5ad --- /dev/null +++ b/snippets/sdbm-hash.md @@ -0,0 +1,13 @@ + +This function implements the **sdbm** hash-algorithm. +It uses the charCodeAt-methode and the shift-operator. + +``` js +function sdbm(s) { + var hashCode = new Number(); + for (var i = 0; i < s.length; i++) { + hashCode = s.charCodeAt(i) + (hashCode << 6) + (hashCode << 16) - hashCode; + } + return hashCode; +} +``` \ No newline at end of file From 5720135525f47087559dbabbea400a7a09139d90 Mon Sep 17 00:00:00 2001 From: Christian Bender Date: Sun, 24 Dec 2017 23:33:52 +0100 Subject: [PATCH 04/10] Update sdbm-hash.md --- snippets/sdbm-hash.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/sdbm-hash.md b/snippets/sdbm-hash.md index e588ee5ad..917d52584 100644 --- a/snippets/sdbm-hash.md +++ b/snippets/sdbm-hash.md @@ -4,10 +4,10 @@ It uses the charCodeAt-methode and the shift-operator. ``` js function sdbm(s) { - var hashCode = new Number(); + var hashCode = 0; for (var i = 0; i < s.length; i++) { hashCode = s.charCodeAt(i) + (hashCode << 6) + (hashCode << 16) - hashCode; } return hashCode; } -``` \ No newline at end of file +``` From 9b24df92e06bba445591cab1cc2aca825bc4280e Mon Sep 17 00:00:00 2001 From: Christian Bender Date: Sun, 24 Dec 2017 23:35:09 +0100 Subject: [PATCH 05/10] Add files via upload --- snippets/sdbm-hash.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/sdbm-hash.md b/snippets/sdbm-hash.md index 917d52584..8f54a20f5 100644 --- a/snippets/sdbm-hash.md +++ b/snippets/sdbm-hash.md @@ -10,4 +10,4 @@ function sdbm(s) { } return hashCode; } -``` +``` \ No newline at end of file From 9d9c460a6866089d7705bbee0f6176c8cc4579b9 Mon Sep 17 00:00:00 2001 From: Christian Bender Date: Mon, 25 Dec 2017 15:36:25 +0100 Subject: [PATCH 06/10] Delete sdbm-hash.md --- snippets/sdbm-hash.md | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 snippets/sdbm-hash.md diff --git a/snippets/sdbm-hash.md b/snippets/sdbm-hash.md deleted file mode 100644 index 8f54a20f5..000000000 --- a/snippets/sdbm-hash.md +++ /dev/null @@ -1,13 +0,0 @@ - -This function implements the **sdbm** hash-algorithm. -It uses the charCodeAt-methode and the shift-operator. - -``` js -function sdbm(s) { - var hashCode = 0; - for (var i = 0; i < s.length; i++) { - hashCode = s.charCodeAt(i) + (hashCode << 6) + (hashCode << 16) - hashCode; - } - return hashCode; -} -``` \ No newline at end of file From 53aa85b1c049ae0c8ec1eaa26ceca7b0607fccce Mon Sep 17 00:00:00 2001 From: Christian Bender Date: Mon, 25 Dec 2017 15:43:38 +0100 Subject: [PATCH 07/10] Add files via upload --- snippets/sdbmHashAlgorithm.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 snippets/sdbmHashAlgorithm.md diff --git a/snippets/sdbmHashAlgorithm.md b/snippets/sdbmHashAlgorithm.md new file mode 100644 index 000000000..1d41ceb8e --- /dev/null +++ b/snippets/sdbmHashAlgorithm.md @@ -0,0 +1,20 @@ +### sdbmHashAlgorithm + +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 in each iteration the 'hashCode'. + +``` js +function sdbm(s) { + let hashCode = 0; + for (let i = 0; i < s.length; i++) { + hashCode = s.charCodeAt(i) + (hashCode << 6) + (hashCode << 16) - hashCode; + } + return hashCode; +} + +// examples +console.log(sdbm("name")) // -3521204949 +console.log(sdbm("age")) // 808122783 +``` + From 152be8fe6c72f048ece491b65761ec7ff1da5f7d Mon Sep 17 00:00:00 2001 From: Soorena Date: Tue, 26 Dec 2017 14:35:31 +0330 Subject: [PATCH 08/10] Update sdbmHashAlgorithm.md trying to make it more like 30-seconds snippets --- snippets/sdbmHashAlgorithm.md | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/snippets/sdbmHashAlgorithm.md b/snippets/sdbmHashAlgorithm.md index 1d41ceb8e..bedc59120 100644 --- a/snippets/sdbmHashAlgorithm.md +++ b/snippets/sdbmHashAlgorithm.md @@ -1,20 +1,19 @@ ### sdbmHashAlgorithm -This algorithm is a simple hash-algorithm that hashs it input string 's' into a whole number. +This algorithm is a simple hash-algorithm that hashes it's input string `s` into a whole number. -The function iterates over each character in string 's' and updates in each iteration the 'hashCode'. +The function iterates over each character in string `s` and updates the `hashCode` in each iteration. ``` js -function sdbm(s) { +const sdbm = s => { let hashCode = 0; for (let i = 0; i < s.length; i++) { hashCode = s.charCodeAt(i) + (hashCode << 6) + (hashCode << 16) - hashCode; } return hashCode; } - -// examples -console.log(sdbm("name")) // -3521204949 -console.log(sdbm("age")) // 808122783 +/* + console.log(sdbm("name")) // -3521204949 + console.log(sdbm("age")) // 808122783 +*/ ``` - From 80b201bc581d1fbf571ff78583931cafa1430ad4 Mon Sep 17 00:00:00 2001 From: Christian Bender Date: Tue, 26 Dec 2017 19:23:01 +0100 Subject: [PATCH 09/10] changed the function changed the function --- snippets/sdbmHashAlgorithm.md | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/snippets/sdbmHashAlgorithm.md b/snippets/sdbmHashAlgorithm.md index bedc59120..6b72e6479 100644 --- a/snippets/sdbmHashAlgorithm.md +++ b/snippets/sdbmHashAlgorithm.md @@ -1,19 +1,19 @@ ### 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 -const sdbm = s => { - let hashCode = 0; - for (let i = 0; i < s.length; i++) { - hashCode = s.charCodeAt(i) + (hashCode << 6) + (hashCode << 16) - hashCode; - } - return hashCode; -} -/* - console.log(sdbm("name")) // -3521204949 - console.log(sdbm("age")) // 808122783 -*/ +const sdbm = str => { + let arr = str.split(''); + return arr.reduce( (hashCode,currentVal) =>{ + return hashCode = currentVal.charCodeAt(0) + (hashCode << 6) + (hashCode << 16) - hashCode; + } + ,0) + } + +// examples +console.log(sdbm("name")) // -3521204949 +console.log(sdbm("age")) // 808122783 ``` + From be32fd4707e6482320635f39db57f8c7e8d33c71 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 27 Dec 2017 10:22:42 +0200 Subject: [PATCH 10/10] Update sdbmHashAlgorithm.md --- snippets/sdbmHashAlgorithm.md | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/snippets/sdbmHashAlgorithm.md b/snippets/sdbmHashAlgorithm.md index 6b72e6479..a77745176 100644 --- a/snippets/sdbmHashAlgorithm.md +++ b/snippets/sdbmHashAlgorithm.md @@ -1,19 +1,17 @@ ### sdbmHashAlgorithm -This algorithm is a simple hash-algorithm that hashs it input string 's' into a whole number. +This algorithm is a simple hash-algorithm that hashes it input string `s` into a whole number. +Use `split('')` and `Array.reduce()` to create a hash of the input string, utilizing bit shifting. ``` js const sdbm = str => { - let arr = str.split(''); - return arr.reduce( (hashCode,currentVal) =>{ - return hashCode = currentVal.charCodeAt(0) + (hashCode << 6) + (hashCode << 16) - hashCode; - } - ,0) - } - -// examples -console.log(sdbm("name")) // -3521204949 -console.log(sdbm("age")) // 808122783 + let arr = str.split(''); + return arr.reduce((hashCode, currentVal) => + hashCode = currentVal.charCodeAt(0) + (hashCode << 6) + (hashCode << 16) - hashCode + ,0) +} +// console.log(sdbm("name")) // -3521204949 +// console.log(sdbm("age")) // 808122783 ```