Map add function question

Tell us what’s happening:

I do not understand why the add function does not work in this code.

Your code so far


let Map = function() {
	this.collection = [];
	this.count = 0;
	this.size = function() {
		return this.count;
	};

	
	this.add = function(key, value) {
		this.collection[key] = value;
		this.count++;
 
	};
	this.has = function(key) {
		return (key in this.collection);
	};
	this.get = function(key) {
		return (key in this.collection) ? this.collection[key] : null;
	};
	this.remove = function(key) {
		if (key in this.collection) {
			delete this.collection[key];
			this.count--;
		}
	};
	this.values = function() {
		let result = new Array();
		for (let key of Object.keys(this.collection)) {
			result.push(this.collection[key]);
		};
		return (result.length > 0) ? result : null;
	};
	this.clear = function() {
		this.collection = {};
		this.count = 0;
	};
};

Your browser information:

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

Challenge: Create a Map Data Structure

Link to the challenge:
https://www.freecodecamp.org/learn/coding-interview-prep/data-structures/create-a-map-data-structure

The add function increments the count by 1 every single time.

Let’s imagine the following scenario:

const myMap = new Map();

myMap.add('first_name', 'Kuincy'); // now count is `1`
myMap.add('last_name', 'Larson'); // now count is `2`

// Oh.. I forgot how to spell Quincy, let's fix this.
myMap.add('first_name', 'Quincy'); // now the count is `3`  <--- this is the issue

I just overrode the existing key first_name and the count was incremented even though it should have stayed the same because I didn’t add any new key.

I understand the functionality here. I got this code from their video on the free code camp youtube channel. They basically walk you through how to build it and show you some example code. I changed the set function to add because that is essentially what is seems like it is doing. It using the index of the array as the key and setting that space in the array equal to the value. Sounds like it’s adding to me. I Just do not understand if there is a glitch in the test or if I am misunderstanding something. That test and only that test fails each time I run the code.

As @husseyexplores was pointing out, your logic is flawed. The add function can be used on an existing property to change it’s value (as shown in @husseyexplores’s example). That doesn’t increase the number of items in the map, but it does increase your count variable.

Am I the only one wondering why you changed this.collection from an Object to an Array? Then you try to assign properties/values to an array?

I may be way off-base, but it seems that might be a direction to consider looking.

1 Like

I was unsure what I was doing when I did that and I was just seeing what it would do if I changed it to an array and forgot to change it back. Either way I still get the same error that it is not properly adding elements to the map. I finally understand what the others are saying though I have to check the collection first to make sure it does not have the same key that I want to add.

Thank you, your example showed me the count should not increment if the a key has previously been used. I was able to solve this problem by adding if statement to check if the key is in the collection and then adding depending on the boolean.

Just as an aside, another way to handle size might be to create an array of Object.keys(this.collection), and return the length of that. Doing so, you don’t have an artificial construct (the this.count property) handling that. By having a property like that, if your internal code is being updated later by someone else, they need to be aware that both the action itself of adding a key/value has to happen, but the count has to be manually updated.

Just a thought, one less thing that could break.

1 Like