GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — JavaScript-Lessons/Data_Structures/Hash_Table/main.js at master · dextel2/JavaScript-Lessons Expand file tree/ Copy pathmain.jsMore file actions 56 lines (43 loc) · 1.33 KB / Copy pathmain.jsFile metadata and controls56 lines (43 loc) · 1.33 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 "use strict"; function HashTable() { var table = []; // generate number based on each char ASCII value that composes the key var loseloseHashCode = function (key) { var hash = 0; for (var i = 0; i < key.length; i++) { hash += key.charCodeAt(i); } return hash % 37; }; // add a new item to the hash table this.put = function (key, value) { var position = loseloseHashCode(key); console.log(position + ' - ' + key); table[position] = value; }; // remove a value from the hash table by accessing desired position and assigning undefined to it this.remove = function (key) { table[loseloseHashCode(key)] = undefined; }; // get value by key this.get = function (key) { return table[loseloseHashCode(key)]; }; this.print = function () { for (var i = 0; i < table.length; ++i) { if(table[i] !== undefined){ console.log(i + ': ' + table[i]); } } } } var hash = new HashTable(); console.log('----------'); console.log(hash.get('Bucky')); console.log(hash.get('Lisa')); console.log(hash.get('Zachary')); console.log('----------'); hash.print(); |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |