JSFiddle - React, Tailwind, and code Playground

by JInks Peng

JavaScript

function Dictionary() {
   this.dataStore = new Array();
   this.add = add;
   this.find = find;
   this.remove = remove;
   this.count = count;
   this.clear = clear;
   this.showSort = showSort;
}
function add(key,value) {
   this.dataStore[key] = value;
}
function find(key) {
   return this.dataStore[key];
}
function remove(key) {
   delete this.dataStore[key];
}
function count() {
   return Object.keys(this.dataStore).length;
}
function clear() {
   var keys =  Object.keys(this.dataStore),key;
   for(var i = 0; i < keys.length; ++i) {
      key = keys[i];
      delete this.dataStore[key];
   }
}
function showSort(check,func) {
   var keys,key;
   if(check) {
      keys =  Object.keys(this.dataStore).sort(func);
   } else {
      keys =  Object.keys(this.dataStore);
   }
   for(var i = 0; i < keys.length; ++i) {
      key = keys[i];
      console.log(key + " -> " + this.dataStore[key]);
   }
}
var pbook = new Dictionary();
pbook.add("Mike",123);
pbook.add("David",345);
pbook.add("Cynthia",456);
pbook.showSort();
console.log(pbook.count());
pbook.showSort(true);
pbook.remove("David");
pbook.showSort();
console.log(pbook.count());