-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maps and Sets Excercises.js
169 lines (133 loc) · 4.97 KB
/
Maps and Sets Excercises.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
class MessageBoard {
/*
In your constructor method, you should assign two properties for each object created from the MessageBoard class. The first should be a property called messages which is an empty Map, and the second is a property called id which has a value of 1.
var m = new MessageBoard
m.hasOwnProperty('messages') // true
m.messages.constructor // function Map() { [native code] }
m.hasOwnProperty('id') // true
m.id // 1
*/
constructor(){
this.messages = new Map
this.id = 1;
}
/*
Add a method called addMessage which accepts a string. The function should add a key and value to the messages map with a key of whatever the value of this.id is and a value of whatever the string is that is passed to the function. The function should return the object created from the class so that the method can be chained. (HINT - to implement the last part, make sure to return this).
var m = new MessageBoard
m.addMessage('hello');
m.messages.size // 1
m.addMessage('awesome!') // m
m.addMessage('awesome!').addMessage('nice!').addMessage('cool!')
*/
addMessage(value){
this.messages.set(this.id, value);
this.id++;
return this;
}
/*
Add a method called findMessageById which accepts a number and returns the message in the messages map with the same key as the number passed to the function. If the key is not found in the messages map, the function should return undefined.
var m = new MessageBoard
m.addMessage('hello!')
m.addMessage('hi!')
m.addMessage('whats up?')
m.findMessageById(1) // 'hello!'
m.findMessageById(2) // 'hi!'
m.findMessageById(3) // 'whats up?'
m.findMessageById(4) // undefined
m.findMessageById() // undefined
*/
findMessageById(id){
return this.messages.get(id);
}
/*
Add a method called findMessageByValue which accepts a string and returns the message in the messages map with the same value as the string passed to the function. If the value is not found in the messages map, the function should return undefined.
var m = new MessageBoard
m.addMessage('hello!')
m.addMessage('hi!')
m.addMessage('whats up?')
m.findMessageByValue('hello!') // 'hello!'
m.findMessageByValue('hi!') // 'hi!'
m.findMessageByValue('whats up?') // 'whats up?'
m.findMessageByValue('nothing here') // undefined
m.findMessageByValue() // undefined
*/
findMessageByValue(val){
for(let msg of this.messages.values()){
if(msg === val) return msg;
}
}
/*
Add a method called removeMessage which accepts a number and removes a message in the messages map with a key of the number passed to the function.
var m = new MessageBoard
m.addMessage('hello!')
m.addMessage('hi!')
m.addMessage('whats up?')
m.removeMessage(1)
m.removeMessage(2)
m.messages.size // 1
m.removeMessage() // m
*/
removeMessage(id){
this.messages.delete(id);
return this;
}
/*
Add a method called numberOfMessages which returns the number of keys in the messages map
var m = new MessageBoard
m.addMessage('hello!')
m.addMessage('hi!')
m.addMessage('whats up?')
m.numberOfMessages() // 3
*/
numberOfMessages(){
return this.messages.size;
}
/*
Add a method called messagesToArray which returns an array of all of the values in the messages map
var m = new MessageBoard
m.addMessage('hello!')
m.addMessage('hi!')
m.addMessage('whats up?')
m.messagesToArray() // ['hello!', 'hi!', 'whats up?'])
*/
messagesToArray(){
return Array.from(this.messages.values());
}
}
/*
Write a function called uniqueValues which accepts an array and returns the number of unique values in the array
uniqueValues([1,1,2,2,2,3,3,3,3,4,4,4,5,5,6]) // 6
*/
function uniqueValues(arr){
return new Set(arr).size;
}
/*
Write a function called hasDuplicates which accepts an array and returns true if there are duplicate values in the array, otherwise it should return false.
hasDuplicates([1,1,2,2,2,3,3,3,3,4,4,4,5,5,6]) // true
hasDuplicates([1,2,3,4,5,6]) // false
hasDuplicates([]) // false
*/
function hasDuplicates(arr){
return new Set(arr).size !== arr.length;
}
/*
Write a function called countPairs which accepts an array of numbers and a number. The function should return the number of unique pairs (two numbers) that sum up to the number passed to the function.
countPairs([8,2,6,4,10,0],10) // 3
countPairs([8,2],10) // 1
countPairs([1,2],10) // 0
countPairs([1,2,3,4,5],10) // 0
countPairs([],10) // 0
countPairs([5,4,-10,6,-20,16],-4) // 2
countPairs([0,-4],-4) // 1
*/
function countPairs(arr, num){
var cache = new Set(arr);
var count = 0;
for(let val of arr){
cache.delete(val);
if(cache.has(num-val)){
count++
}
}
return count;
}