-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforeach-map-filter.js
283 lines (238 loc) · 7.68 KB
/
foreach-map-filter.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
Write a function called doubleValues which accepts an array and returns a new array with all the values
in the array passed to the function doubled
Examples:
doubleValues([1,2,3]) // [2,4,6]
doubleValues([5,1,2,3,10]) // [10,2,4,6,20]
*/
function doubleValues(arr){
const result = [];
arr.forEach(
function(val){
result.push(val * 2);
}
)
return result;
}
/*
Write a function called onlyEvenValues which accepts an array and returns a new array with only the even
values in the array passed to the function
Examples:
onlyEvenValues([1,2,3]) // [2]
onlyEvenValues([5,1,2,3,10]) // [2,10]
*/
function onlyEvenValues(arr){
const result = arr.filter(
function(val){
return (val % 2 === 0)
}
);
return result;
}
/*
Write a function called showFirstAndLast which accepts an array of strings and returns a new array with
only the first and last character of each string.
Examples:
showFirstAndLast(['colt','matt', 'tim', 'test']) // ["ct", "mt", "tm", "tt"]
showFirstAndLast(['hi', 'goodbye', 'smile']) // ['hi', 'ge', 'se']
*/
function showFirstAndLast(arr){
const result = arr.map(
function(val){
return val[0] + val[val.length - 1]
}
);
return result;
}
/*
Write a function called addKeyAndValue which accepts an array of objects, a key, and a value and returns
the array passed to the function with the new key and value added for each object
Examples:
addKeyAndValue([{name: 'Elie'}, {name: 'Tim'}, {name: 'Matt'}, {name: 'Colt'}], 'title', 'instructor')
// [{name: 'Elie', title:'instructor'}, {name: 'Tim', title:'instructor'}, {name: 'Matt', title:'instructor'}, {name: 'Colt', title:'instructor'}]
*/
function addKeyAndValue(arr,key,value){
const result = arr.map(
function(obj){
obj[key] = value;
return obj;
}
);
return result;
}
/*
Write a function called vowelCount which accepts a string and returns an object with the keys as the vowel
and the values as the number of times the vowel appears in the string. This function should be case insensitive
so a lowercase letter and uppercase letter should count.
Examples:
vowelCount('Elie') // {e:2,i:1};
vowelCount('Tim') // {i:1};
vowelCount('Matt') // {a:1})
vowelCount('hmmm') // {};
vowelCount('I Am awesome and so are you') // {i: 1, a: 4, e: 3, o: 3, u: 1};
*/
function vowelCount(str){
const vowelsOnly = [...str].filter(
function(char){
return ('aeiou'.indexOf(char.toLowerCase()) !== -1)
}
)
const result = {};
if (vowelsOnly.length === 0) return result;
vowelsOnly.forEach(
function(letter){
const caseInsen = letter.toLowerCase();
if (result[caseInsen]){
result[caseInsen] += 1;
} else {
result[caseInsen] = 1;
}
}
);
return result;
}
/*
Write a function called doubleValuesWithMap which accepts an array and returns a new array with
all the values in the array passed to the function doubled
Examples:
doubleValuesWithMap([1,2,3]) // [2,4,6]
doubleValuesWithMap([1,-2,-3]) // [2,-4,-6]
*/
function doubleValuesWithMap(arr) {
const result = arr.map(
function(val){
return val * 2;
}
);
return result;
}
/*
Write a function called valTimesIndex which accepts an array and returns a new array with each
value multiplied by the index it is currently at in the array.
Examples:
valTimesIndex([1,2,3]) // [0,2,6]
valTimesIndex([1,-2,-3]) // [0,-2,-6]
*/
function valTimesIndex(arr){
const result = arr.map(
function(val, i){
return(val * i);
}
);
return result;
}
/*
Write a function called extractKey which accepts an array of objects and some key and return
a new array with the value of that key in each object.
Examples:
extractKey([{name: 'Elie'}, {name: 'Tim'}, {name: 'Matt'}, {name: 'Colt'}], 'name') // ['Elie', 'Tim', 'Matt', 'Colt']
*/
function extractKey(arr, key){
const result = arr.map(
function(obj){
return obj[key];
}
);
return result;
}
/*
Write a function called extractFullName which accepts an array of objects andreturns a new array
with the value of the key with a name of "first" and the value of a key with the name of "last"
in each object, concatenated together with a space.
Examples:
extractFullName([{first: 'Elie', last:"Schoppik"}, {first: 'Tim', last:"Garcia"}, {first: 'Matt', last:"Lane"}, {first: 'Colt', last:"Steele"}]) // ['Elie Schoppik', 'Tim Garcia', 'Matt Lane', 'Colt Steele']
*/
function extractFullName(arr){
const result = arr.map(
function(obj){
return `${obj.first} ${obj.last}`
}
);
return result;
}
/*
Write a function called filterByValue which accepts an array of objects and a key and returns a new
array with all the objects that contain that key.
Examples:
filterByValue([{first: 'Elie', last:"Schoppik"}, {first: 'Tim', last:"Garcia", isCatOwner: true}, {first: 'Matt', last:"Lane"}, {first: 'Colt', last:"Steele", isCatOwner: true}], 'isCatOwner') // [{first: 'Tim', last:"Garcia", isCatOwner: true}, {first: 'Colt', last:"Steele", isCatOwner: true}]
*/
function filterByValue(arr, key) {
const result = arr.filter(
function(obj){
return (obj[key] !== undefined)
}
);
return result;
}
/*
Write a function called find which accepts an array and a value and returns the first element
in the array that has the same value as the second parameter or undefined if the value is not found in the array.
Examples:
find([1,2,3,4,5], 3) // 3
find([1,2,3,4,5], 10) // undefined
*/
function find(arr, searchValue) {
const result = arr.filter(
function(val){
return (val === searchValue);
}
);
if (result.length === 0){
return undefined;
} else {
return result[0];
}
}
/*
Write a function called findInObj which accepts an array of objects, a key, and some value to search for
and returns the first found value in the array.
Examples:
findInObj([{first: 'Elie', last:"Schoppik"}, {first: 'Tim', last:"Garcia", isCatOwner: true}, {first: 'Matt', last:"Lane"}, {first: 'Colt', last:"Steele", isCatOwner: true}], 'isCatOwner',true) // {first: 'Tim', last:"Garcia", isCatOwner: true}
*/
function findInObj(arr, key, searchValue) {
const result = arr.filter(
function(obj){
return obj[key] === searchValue;
}
)
return result[0];
}
/*
Write a function called removeVowels which accepts a string and returns a new string with all of the vowels
(both uppercased and lowercased) removed. Every character in the new string should be lowercased.
Examples:
removeVowels('Elie') // ('l')
removeVowels('TIM') // ('tm')
removeVowels('ZZZZZZ') // ('zzzzzz')
*/
function removeVowels(str) {
let noVowels = [...str].filter(
function(char){
return ('aeiou'.indexOf(char.toLowerCase()) === -1);
}
).map(
function(char){
return char.toLowerCase();
}
)
return noVowels.join('');
}
/*
Write a function called doubleOddNumbers which accepts an array and returns a new array with all
of the odd numbers doubled (HINT - you can use map and filter to double and then filter the odd numbers).
Examples:
doubleOddNumbers([1,2,3,4,5]) // [2,6,10]
doubleOddNumbers([4,4,4,4,4]) // []
*/
function doubleOddNumbers(arr) {
const odds = arr.filter(
function(num){
return (num % 2 !== 0);
}
).map(
function(num){
return num * 2;
}
);
return odds;
}