added:solution for Coding Interview Prep/Data Structures-Create a Map Data Structure (#38337)

* add: solution to Data Structures-Create a Map Data Structure

* Proper usage of -hasOwnProperty

removed unnecessary returns
This commit is contained in:
Shivam
2020-03-08 11:37:40 +05:30
committed by GitHub
parent f736e67537
commit dbf3815fc7

View File

@ -70,7 +70,41 @@ var Map = function() {
<section id='solution'>
```js
// solution required
var Map = function() {
this.collection = {};
// change code below this line
this.add = function(key,value) {
this.collection[key] = value;
}
this.remove = function(key) {
delete this.collection[key];
}
this.get = function(key) {
return this.collection[key];
}
this.has = function(key) {
return this.collection.hasOwnProperty(key)
}
this.values = function() {
return Object.values(this.collection);
}
this.size = function() {
return Object.keys(this.collection).length;
}
this.clear = function() {
for(let item of Object.keys(this.collection)) {
delete this.collection[item];
}
}
// change code above this line
};
```
</section>