-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdictionary(map).js
More file actions
73 lines (59 loc) · 2.07 KB
/
dictionary(map).js
File metadata and controls
73 lines (59 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
In a dictionary(or map), we store values in pairs as ([key, value]).
the key is used to find the particular element.
*/
// CREATING A DICTIONARY CLASS
function Dictionary () {
var items = {};
/**
* we will need the following methods in our dictionary/map
* set(key, value) - this adds a new item to the dictionary.
* delete(key) - this removes the value from the dictionary using the key
* has(key) - this returns true if thet key exists in the dictionary and false otherwise
* get(key) - this returns a specific value searched by the key
* clear() - this removes all the items in the dictionary/map
* size() - this returns how may elements the dictionary contains
* keys() - this returns an array of all the keys the dictionary contains
* values() - this returns an array of all the values of the dictionary
*/
// the has() method
// we implement this method because it is used in other methods
this.has = function (key) {
return key in items;
};
//the set() method
this.set = function (key, value) {
items[key] = value;
}
//the delete() method
this.delete = function (key) {
if (this.has(key)) {
delete items[key];
return true;
}
return false;
}
// the get() method
this.get = function (key) {
return this.has(key) ? items[key] : undefined;
};
// the values() method
this.values = function () {
let values = [];
for (var k in items) {
if (this.has(k)) {
values.push(items[k]);
}
}
return values;
}
// the clear() and size() methods are exactly the same as those of the Set class
// the keys() class
this.keys = function () {
return Object.keys(items);
}
//getItems() method
this.getItems = function () {
return items;
}
}