Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function hashTable (){
- this.size = 0;
- this.dataStore = {};
- }
- hashTable.prototype.toString = function(){
- return this.dataStore.toString();
- }
- hashTable.prototype.enumerate = function(){
- for(var element in this.dataStore){
- console.log(element + " " + this.dataStore[element])
- }
- return this.dataStore;
- }
- hashTable.prototype.remove = function(key){
- if(this.dataStore.hasOwnProperty(key)){
- this.size -= 1;
- delete this.dataStore[key];
- }
- }
- hashTable.prototype.size = function(){
- return this.size;
- }
- hashTable.prototype.put = function (key, value){
- if(this.dataStore.hasOwnProperty(key)){
- throw new error ("hashtable cannot contain duplicates")
- }
- else {
- this.dataStore[key] = value;
- this.size += 1;
- return this.dataStore;
- }
- }
- hashTable.prototype.clear = function(){
- this.dataStore = {};
- this.size = 0;
- return this.dataStore;
- }
- hashTable.prototype.isEmpty = function(){
- return this.size > 0 ? false : true;
- }
- hashTable.prototype.contains = function(key){
- if(this.dataStore.hasOwnProperty(key)){
- return true;
- }
- else {
- return false;
- }
- }
- var ht = new hashTable();
- console.log(ht.put("color", "red"))
- ht.put("bike", "blue");
- console.log(ht.isEmpty())
- console.log(ht.remove("bike"))
- console.log(ht.size)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement