Create-a-map-data-structure

Tell us what’s happening:

Hello All,

This code works fine on my IDE, but the 3rd test of “add” fails in fcc sandbox. I don’t know why.I also checked the seed code on github, the 3rd assert instruction read :

“assert((function() { var test = false; if (typeof Map !== ‘undefined’) { test = new Map() }; test.add(5,6); test.add(2,3); test.add(2,5); return (test.size() == 3)})(), ‘message: The add method adds items to the map.’);”

It adds (5,6),(2,3),(2,5) … then why is it asserting test.size() = 3 ? Shouldn’t it be = 2, since (2,3) and (2,5) will still map to the same key : 2 …

Please let me know, where I am going wrong.
Thanks.

Your code so far



var Map = function() {
  this.collection = {};
  // change code below this line
  this.add = (key,value)=>{
    this.collection[key] = value;
    return this.collection[key];
  };
  this.remove = (key)=>{
    if(this.collection.hasOwnProperty(key)){
      this.collection.delete(key);
    }
  };
  this.get = (key)=>{
    return this.collection[key];
  };
  this.has = (item)=>{
    return (this.collection.hasOwnProperty(item));
 };
 this.values = ()=>{
   let ar = [];
   for(let v in this.collection){
	   ar.push(this.collection[v]);
   }
   return ar;
 }; 
  this.size = ()=>(Object.keys(this.collection).length);
  this.clear = ()=>{
    this.collection = {};
  };
  // change code above this line
};

var m1 = new Map();
m1.add("p1","Tom");
m1.add("p2","Jerry");
m1.add("p3","Julie");
console.log(m1.values());
console.log(m1.size());
m1.clear();
m1.add(2,3);
m1.add(2,5);
m1.add(3,6);
console.log(m1.values());
console.log(m1.size());

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0.

Link to the challenge:

I suspect that calling add with the same key value was a mistake. (Really, I would say that trying to add a key a second time should probably return an error instead of overwriting the existing value, but that’s just me.)

You can create a GitHub Issue pointing out that either the test is incorrect or something is missing from the instructions.

Calling a standard js map also doesn’t throw any error, only updates to new value for the key. Thank you for your reply and suggestion.

Is this issue solved?
this is April-19 and i am getting same error.

The add method adds items to the map.

you can use has method inside the add method to check if collection has that key.

if(!this.has(key)){
      this.collection[key] = value;
    }