-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap-practice.html
More file actions
131 lines (112 loc) · 3.97 KB
/
map-practice.html
File metadata and controls
131 lines (112 loc) · 3.97 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Map, Filter, Reduce Mini-Exercises</title>
</head>
<body>
<script>
"use strict";
// TODO: Given the following array, complete the todos...
const dogs = [
{
dogName: 'Bubbles',
age: 10,
isTrained: false
},
{
dogName: 'Lexie',
age: 3,
isTrained: true
},
{
dogName: 'Doggy',
age: 5,
isTrained: false
},
{
dogName: 'Flopper',
age: 3,
isTrained: true
},
{
dogName: 'Lexie',
age: 1,
isTrained: true
},
{
dogName: 'Skip',
age: 7,
isTrained: true
},
{
dogName: 'Blue',
age: 4,
isTrained: false
}
];
// MAP
// TODO 1: using map, create a new array of dog names from the dogs array
console.log('Exercise 1:');
const dogNames = dogs.map ((dogs) => dogs.dogName)
console.log(dogNames)
// TODO 2: using map, create a new array of dog ages from the dogs array
console.log('Exercise 2:');
//const dogAges = dogs.map (({age}) => age)
// or //
const dogAges = dogs.map ((dogs) => dogs.age)
console.log(dogAges)
// TODO 3: using map, create a new array of dog objects from the dogs array that only have dog names and age properties and values
console.log('Exercise 3:');
const dogObjects = ({dogName : name, age}) => {
return {
name, age
}
}
const dogsRemap = dogs.map(dogObjects)
console.log(dogsRemap)
// FILTER
// TODO 4: using filter, create a new array containing only dogs younger than 10 years old
console.log('Exercise 4:');
const dogsLessThanTen = dogs.filter ((dogs) => dogs.age < 10)
console.log(dogsLessThanTen)
// TODO 5: using filter, create a new array containing only dogs named 'Lexie'
console.log('Exercise 5:');
const dogNamedLexi = dogs.filter ((dogs) => dogs.dogName === 'Lexie')
console.log(dogNamedLexi)
// TODO 6: using filter, create a new array containing only dogs that are trained and younger than 10
console.log('Exercise 6:');
const trainedDogs = dogs.filter ((dogs) => dogs.isTrained = true && dogs.age < 10)
console.log(trainedDogs)
// REDUCE
// TODO 7: using reduce, return a string containing all dog names together with no spaces ("BubblesLexieDoggy...")
console.log('Exercise 7:');
// const stringOfNames = dogNames.join('')
// console.log(stringOfNames)
const dogNameString = dogs.reduce((previousValue, currentValue) => previousValue+currentValue.dogName, "")
console.log(dogNameString)
// TODO 8: using reduce, return the total of adding all dog ages together (33)
console.log('Exercise 8:');
const addDogAges = dogs.reduce((previousValue, currentValue) => previousValue+currentValue.age, 0)
console.log(addDogAges)
// TODO 9: using reduce, return an array of dog objects with all isTrained properties set to true
console.log('Exercise 9:');
const trainedDogsArray = dogs.reduce((previousValue, currentValue) => {
if(currentValue.isTrained) previousValue.push(currentValue)
return previousValue
},[])
console.log(trainedDogsArray)
// EXTRA CHALLENGES
// TODO 10: what is the average age of each dog?
console.log('Exercise 10:');
// TODO 11: what is the average age of dogs that are trained?
console.log('Exercise 11:');
// TODO 12: what is the average length of names of untrained dogs?
console.log('Exercise 12:');
// TODO 13: what are the combined ages of all dogs in dog years? (7x more than a human year)
console.log('Exercise 13:');
// TODO 14: create a string of the first letters of each dog name for dogs three years old (should be "LF")
console.log('Exercise 14:');
</script>
</body>
</html>