-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathes6_Bonus.html
More file actions
299 lines (228 loc) · 9.49 KB
/
es6_Bonus.html
File metadata and controls
299 lines (228 loc) · 9.49 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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
<title>Map Filter Reduce</title>
</head>
<body>
<main class="container">
<h1>Map, Filter, Reduce Practice Problems</h1>
</main>
<!-- Custom JS -->
<script>
{
"use strict";
const fruits = ["cantaloupe", "orange", "date", "elderberry", "ugli fruit", "pineapple"];
const customers = [
{
name: "Fred",
age: 58,
occupation: "Police Officer",
noOfPurchases: 4
},
{
name: "Samantha",
age: 54,
occupation: "Teacher",
noOfPurchases: 18
},
{
name: "Charles",
age: 38,
occupation: "Librarian",
noOfPurchases: 9
}
];
const pets = [
{
name: 'Bud',
age: 2,
breed: 'Pug'
},
{
name: 'Gabby',
age: 10,
breed: 'Retriever'
},
{
name: 'Fred',
age: 1,
breed: 'Lab'
},
{
name: 'Bowser',
age: 2,
breed: 'Pug'
}
];
const family = [
{
name: "Pam",
gender: "female",
age: 29,
},
{
name: "Amelie",
gender: "female",
age: 10,
},
{
name: "Justin",
gender: "male",
age: 32,
},
];
// PROBLEM 1 - create an array of the first letters of each fruit
let firstLetter = fruits.map(fruit => fruit.charAt(0));
console.log(firstLetter);
// PROBLEM 2 - create array of user objects based on the customers array
// of objects (each user object should just have name and age properties)
let newCustomerList = customers.map(person => {
const {name, age} = person;
return {name, age};
}, {});
console.log(newCustomerList);
// PROBLEM 3 - create an array of civil servant customers (teachers and police officers)
// containing the same properties as the objects on the customers objects
let civilServantList = customers.filter(customer => (customer.occupation === "Teacher" || customer.occupation == "Police Officer"))
.map(person => {
const {name, age} = person;
return {name, age};
}, {});
console.log(civilServantList);
// PROBLEM 4 - determine the average age of customers
const averageAge = customers.reduce((totalAge, customer) => totalAge += customer.age, 0);
console.log(averageAge / customers.length);
// PROBLEM 5 - create a function makeSuperPet() that takes in the pets array as input and returns a single pet object
// with the following shape...
/*
{
name: ALL_PET_NAMES_CONCATENATED_INTO_A_SINGLE_STRING,
age: THE_TOTAL_OF_ALL_PET_AGES,
breed: THE_FIRST_LETTERS_OF_ALL_PET_BREEDS_CONCATENATATED_INTO_A_SINGLE_STRING
}
*/
/**
*
*
*
let salesReport = salesPeople.reduce((finalString, person) => finalString + `${person.name} has sold ${person.units} units this quarter! `, "");
console.log(salesReport);
*/
function makeSuperPet(pets) {
return {
name: pets.reduce((finalName, pet) => finalName + pet.name, ""),
age: pets.reduce((totalAge, pet) => totalAge + pet.age, 0),
breed: pets.reduce((newBreed, pet) => newBreed + pet.breed.charAt(0), "")
}
}
console.log(makeSuperPet(pets));
// PROBLEM 6 - take in an array of pets and return an array of the length of first names for pugs only
// your output for the given input should be [3, 6] for 'Bud' and 'Bowser'
const newPetArray = pets.filter(pet => pet.breed === "Pug");
console.log(newPetArray);
// PROBLEM 7 - create a function getFemaleFamilyMembers() that when given the family variable as an argument,
// returns an array of female family member names
function getFemaleFamilyMembers(family) {
return family.filter(familyMember => (familyMember.gender === "female")).map(person => person.name);
};
console.log(getFemaleFamilyMembers(family));
// PROBLEM 8 - create a function makeLongPetString() that when given the variable of pets,
// returns a string of all property values with dashes separating each property value
function makeLongPetString(pets) {
return pets.map(pet => `${pet.name} - ${pet.age} - ${pet.breed}`);
}
console.log(makeLongPetString(pets));
// PROBLEM 9 - create a function that when given an array of first names, returns an array of the same names with a last name of Smith
// input = ['Sally', 'Fred', 'Steve']
// output = ['Sally Smith', 'Fred Smith', 'Steve']
function addLastName(input) {
return input.map(name => `${name} Smith`);
}
console.log(addLastName(['Sally', 'Fred', 'Steve']));
// PROBLEM 10 - create a function that when given an array of numbers, returns the sum of even numbers
function addEven(numbers){
return numbers.reduce((total, number) => {
if (number % 2 === 0){
total += number;
}
return total;
}, 0);
}
console.log(addEven([1, 6, 10, 30, 40, 45]));
// PROBLEM 11 - create a function that when given an array of numbers, returns the sum of all numbers evenly divisible by 10
function addDivisible10(numbers){
return numbers.reduce((total, number) => {
if (number % 10 === 0){
total += number;
}
return total;
}, 0);
}
console.log(addDivisible10([1, 6, 10, 30, 40, 45]));
// PROBLEM 12 - create a function that when given an array of names, returns a string of all the first letters of each name
function firstLetterName(names){
return names.reduce((newString, names) => {
return newString += names.charAt(0)
}, "");
}
console.log(firstLetterName(["Mary", "John", "Peter", "Gabriel", "Camilo", "Rufino"]));
// PROBLEM 13 - create a function that when given an array of values, returns an array of only the truthy values
function truthyValues(values) {
return values.reduce((returnArray, value) => {
if (Boolean(value)){
returnArray.push(value);
}
return returnArray;
}, []);
}
console.log(truthyValues([0,false,true,1,"hello"]));
// = users.reduce((listOfLangs, user) => {
// for (const lang of user.languages) {
// if (listOfLangs.indexOf(lang) === -1) {
// listOfLangs.push(lang);
// }
// }
//
// return listOfLangs;
// }, []);
// }
// PROBLEM 14 - create a function that when given an object, returns the property values as an array of elements
function objectValues(inputObject){
return Object.values(inputObject);
}
console.log(objectValues({
name: "Amelie",
gender: "female",
age: 10,
}));
// PROBLEM 15 - create a function that when given an object, returns the property values as an array of elements
// PROBLEM 16 - create a function that when given three arguments: a min num, a max num, an array
// of nums will return the array of nums that are only between the min and max values, inclusive
function valuesBetween(min, max, inputArray){
return inputArray.filter(currentNumber => currentNumber > min && currentNumber < max);
// return family.filter(familyMember => (familyMember.gender === "female")).map(person => person.name);
}
console.log(valuesBetween(5, 50, [0, 1, 2, 3, 4, 5, 10, 15, 20, 45, 50, 55]));
// PROBLEM 17 - create a function that when given an array of strings, returns an array of objects
// with properties for the given string value and the length of the string and the string without vowels (not including y)
function createObject(propertyValues){
let newObjectString = "{ ";
for (let index = 0; index < propertyValues.length; index++){
if (index < propertyValues.length - 1){
newObjectString += "property"+index + ": " + propertyValues[index] + ",";
}
else {
newObjectString += "property"+index + ": " + propertyValues[index];
}
}
newObjectString += "} ";
return newObjectString;
}
console.log(createObject(["one", "two", "three"]));
}
</script>
</body>
</html>