/
exercise4.js
51 lines (51 loc) · 1.69 KB
/
exercise4.js
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
"use strict";
console.log("---------- This is Exercise 4 -------------");
// Create a generic Map (an Object like an Array, but instead with Key-Value Pairs). The key will always be a string.
// Let's keep it simple and only add the following methods to the Map:
// setItem(key: string, item: T) // should create a new key-value pair
// getItem(key: string) // should retrieve the value of the provided key
// clear() // should remove all key-value pairs
// printMap() // should output key-value pairs
// The map should be usable like shown below:
// const numberMap = new MyMap<number>();
// numberMap.setItem('apples', 5);
// numberMap.setItem('bananas', 10);
// numberMap.printMap();
// const stringMap = new MyMap<string>();
// stringMap.setItem('name', "Max");
// stringMap.setItem('age', "27");
// stringMap.printMap();
var myMap = /** @class */ (function () {
function myMap() {
this.map = {};
}
myMap.prototype.setItem = function (key, item) {
this.map[key] = item;
};
myMap.prototype.getItem = function (key) {
return this.map[key];
};
myMap.prototype.clear = function () {
this.map = {};
};
myMap.prototype.printMap = function () {
for (var key in this.map) {
console.log(key, this.map[key]);
}
};
return myMap;
}());
var numberMap = new myMap();
numberMap.setItem("apple", 5);
numberMap.setItem("bananas", 10);
console.log(numberMap.getItem("apple"));
numberMap.printMap();
numberMap.clear();
numberMap.printMap();
var stringMap = new myMap();
stringMap.setItem("apple", "120");
stringMap.setItem("bananas", "212");
console.log(stringMap.getItem("apple"));
stringMap.printMap();
stringMap.clear();
stringMap.printMap();