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:
@ -70,7 +70,41 @@ var Map = function() {
|
|||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```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>
|
</section>
|
||||||
|
Reference in New Issue
Block a user