Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. function hashTable (){
  2. this.size = 0;
  3. this.dataStore = {};
  4. }
  5.  
  6. hashTable.prototype.toString = function(){
  7. return this.dataStore.toString();
  8. }
  9.  
  10. hashTable.prototype.enumerate = function(){
  11. for(var element in this.dataStore){
  12. console.log(element + " " + this.dataStore[element])
  13. }
  14. return this.dataStore;
  15. }
  16. hashTable.prototype.remove = function(key){
  17. if(this.dataStore.hasOwnProperty(key)){
  18. this.size -= 1;
  19. delete this.dataStore[key];
  20. }
  21. }
  22. hashTable.prototype.size = function(){
  23. return this.size;
  24. }
  25.  
  26. hashTable.prototype.put = function (key, value){
  27. if(this.dataStore.hasOwnProperty(key)){
  28. throw new error ("hashtable cannot contain duplicates")
  29. }
  30. else {
  31. this.dataStore[key] = value;
  32. this.size += 1;
  33. return this.dataStore;
  34. }
  35. }
  36.  
  37. hashTable.prototype.clear = function(){
  38. this.dataStore = {};
  39. this.size = 0;
  40. return this.dataStore;
  41. }
  42.  
  43. hashTable.prototype.isEmpty = function(){
  44. return this.size > 0 ? false : true;
  45. }
  46.  
  47. hashTable.prototype.contains = function(key){
  48. if(this.dataStore.hasOwnProperty(key)){
  49. return true;
  50. }
  51. else {
  52. return false;
  53. }
  54. }
  55. var ht = new hashTable();
  56. console.log(ht.put("color", "red"))
  57. ht.put("bike", "blue");
  58. console.log(ht.isEmpty())
  59.  
  60. console.log(ht.remove("bike"))
  61. console.log(ht.size)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement