Create a Map Data Structure*

What is the reason why this is not working?

  **Your code so far**

var Map = function() {
this.collection = {};
this.size = () => {
  return Object.entries(this.collection).length
}
this.has = (key) =>{
  let keysOfCollection = Object.keys(this.collection)
  
  if(keysOfCollection.includes(key)){
     return true
  }else{
     return false
  }
 this.values = function (){
   return Object.values(this.collection)
 } 
}
 this.get = function (key) {
   let entriesOfCollection=Object.entries(this.collection)  
      for(let i=0;i<entriesOfCollection.length;i++){
              for(let j=0;j<entriesOfCollection[i].length;j++){
                         if(entriesOfCollection[i][0]===key){
                                   return entriesOfCollection[i][1]
                         }
             }
       }   
  }
  this.clear = function(){
    return this.collection={}
  } 

};
let a = new Map()


  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36

Challenge: Create a Map Data Structure

Link to the challenge:

You are missing the add, and remove methods. You don’t actually need to have the remove method do anything because the test only checks that it’s there not what it does.

Your values method is nested in your has method so you just need to put it in the outer function.

1 Like
var Map = function() {
  this.collection = {};
  this.add = function (key,value) {
       this.collection[key]=value
  }
  this.remove = function (key){
       delete this.collection[key]
  }
  this.get = function (key){
       let arrayOfEntries=Object.entries(this.collection)
       for(let i=0;i<arrayOfEntries.length;i++){
             for(let j=0;j<arrayOfEntries[i].length;j++){
                          if(arrayOfEntries[i][0]===key){
                                 return arrayOfEntries[i][1]
                          }
             }  
       }  
  }
  this.has=function(key){
        if(this.collection.hasOwnProperty(key)){
                  return true
        }
        return false
  }
  this.values=function (){
        return Object.values(this.collection)
  }
  this.size = function (){
       return Object.keys(this.collection).length
  }
  this.clear = function (){
        this.collection={}
  }
};
let a = new Map ()

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.