Implement a simple store class (hashSet) in JavaScript with set(key, value), get(key), & has(key) methods.
Example
const store = new Store(); store.set('a', 10); store.set('b', 20); store.set('c', 30); store.get('b'); // 20 store.has('c'); // true
Reading the problem statement we can derive that we have to create a simple function that will store the list of key-value pairs and will expose two methods, one to check if the key exists in the store and the second to get the value associated with the key, apart from these one more method to store the key-values.
We can do this by simply creating a function with an object that will store the key-value and these methods.
const Store = function(){ //store the data this.list = {}; //set the key-value in list this.set = function(key, value){ this.list[key] = value; } //get the value of the given key this.get = function(key){ return this.list[key]; } //check if key exists this.has = function(key){ return !!this.list[key]; } }
Input: const store = new Store(); store.set('a', 10); store.set('b', 20); store.set('c', 30); console.log(store.get('b')); console.log(store.has('c')); console.log(store.get('d')); console.log(store.has('e')); Output: 20 true undefined false