Problems in Map implementation

Tell us what’s happening:
I console.loged all inputs but I’m unable to pass the tests for add , clear and values methods.

I think my code is alright . Please help me figure out what I’m doing wrong.

Your code so far


var Map = function() {
this.collection = {};
// Only change code below this line
this.length = 0;
this.print = function(){
  return this.collection;
}
this.add = function(key,value){
  this.collection[key] = value;
  this.length++
}

this.remove = function(key){
  delete this.collection[key];
  this.length--;
}

this.get = function(key){
  return this.collection[key];
}

this.has = function(key){
  if(this.collection[key]){
    return true;
  }
  return false;
}

this.values = function(){
     return Object.values(this.collection);
}

this.size = function(){
  return Object.values(this.length);
}

this.clear = function(){
  return (this.collection = {});
}


// Only change code above this line
};

var map = new Map();
map.add(1,3);
map.add(2,5);
map.add(3,4)
console.log(map)
console.log(map.size())

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0.

Challenge: Create a Map Data Structure

Link to the challenge:

hey @sanadKadu,

This will return a string when its expecting an array, a better way to do this would be to use Object.values(this.collection) to return an array of values. and for your size function you could use the same and just add .length to the end

1 Like

thanks again mate. But I am also getting error with my clear and add method but if I try to find what went wrong using console.log it all seems fine .

@sanadKadu

this is wrong it should be Object.values(this.collection).length

1 Like

you’re a saviour, my friend. I’m really sorry if I’m annoying you , Thanks again.

1 Like

no not annoying me lol