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:
add
accepts akey, value
pair to add to the map.remove
accepts a key and removes the associatedkey, value
pairget
accepts akey
and returns the storedvalue
has
accepts akey
and returns true if the key exists or false if it doesn't.values
returns an array of all the values in the mapsize
returns the number of items in the mapclear
empties the map
Tests
- Waiting: 1. The
Map
data structure should exist. - Waiting: 2. The
Map
object should have the following methods:add
,remove
,get
,has
,values
,clear
, andsize
. - Waiting: 3. The
add
method should add items to the map. - Waiting: 4. The
has
method should returntrue
for added items andfalse
for absent items. - Waiting: 5. The
get
method should accept keys as input and should return the associated values. - Waiting: 6. The
values
method should return all the values stored in the map as strings in an array. - Waiting: 7. The
clear
method should empty the map and thesize
method should return the number of items present in the map.
/** * Your test output will go here */