Create a Map Data Structure
The next few challenges will cover maps and hash tables. Maps are data structures that store key-value pairs. In JavaScript, these are available to us as objects. Maps provide rapid lookup of stored items based on key values and are very common and useful data structures.
Let's get some practice creating our own map. Because JavaScript objects provide a much more efficient map structure than anything we could write here, this is intended primarily as a learning exercise. However, JavaScript objects only provide us with certain operations. What if we wanted to define custom operations? Use the Map object provided here as a wrapper around a JavaScript object. Create the following methods and operations on the Map object:
addaccepts akey, valuepair to add to the map.removeaccepts a key and removes the associatedkey, valuepairgetaccepts akeyand returns the storedvaluehasaccepts akeyand returns true if the key exists or false if it doesn't.valuesreturns an array of all the values in the mapsizereturns the number of items in the mapclearempties the map
Tests
- Waiting: 1. The
Mapdata structure should exist. - Waiting: 2. The
Mapobject should have the following methods:add,remove,get,has,values,clear, andsize. - Waiting: 3. The
addmethod should add items to the map. - Waiting: 4. The
hasmethod should returntruefor added items andfalsefor absent items. - Waiting: 5. The
getmethod should accept keys as input and should return the associated values. - Waiting: 6. The
valuesmethod should return all the values stored in the map as strings in an array. - Waiting: 7. The
clearmethod should empty the map and thesizemethod should return the number of items present in the map.
/** * Your test output will go here */