-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataSheet.js
More file actions
183 lines (158 loc) · 4.56 KB
/
dataSheet.js
File metadata and controls
183 lines (158 loc) · 4.56 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
const characters = [{
name: 'Luke Skywalker',
height: 172,
mass: 77,
eye_color: 'blue',
gender: 'male',
},
{
name: 'Darth Vader',
height: 202,
mass: 136,
eye_color: 'yellow',
gender: 'male',
},
{
name: 'Leia Organa',
height: 150,
mass: 49,
eye_color: 'brown',
gender: 'female',
},
{
name: 'Anakin Skywalker',
height: 188,
mass: 84,
eye_color: 'blue',
gender: 'male',
},
];
//***MAP***
//1. Get array of all names
const allNames = characters.map(character => (character.name));
// console.log(allNames);
//2. Get array of all heights
const allHeights = characters.map(character => (character.height));
// console.log(allHeights);
//3. Get array of objects with just name and height properties
const nameAndHeight = characters.map(character => ({
name: character.name,
height: character.height
}));
// console.log(nameAndHeight);
//4. Get array of all first names
const allFirstNames = characters.map(character => (
character.name.split(' ')[0]
));
// console.log(allFirstNames);
//***REDUCE***
//1. Get total mass of all characters
const totalMass = characters.reduce((acc, cur) => (
acc + cur.mass
), 0);
// console.log(totalMass);
//2. Get total height of all characters
const totalHeight = characters.reduce((acc, cur) => (
acc + cur.height
), 0);
// console.log(totalHeight);
//3. Get total number of characters by eye color
const charactersByEyeColor = characters.reduce((acc, cur) => {
const color = cur.eye_color;
if (acc[color]) {
acc[color]++;
} else {
acc[color] = 1;
}
return acc;
}, {});
// console.log(charactersByEyeColor);
//4. Get total number of characters in all the character names
const totalNameCharacters = characters.reduce((acc, cur) => (
acc + cur.name.length
), 0);
// console.log(totalNameCharacters);
//***FILTER***
//1. Get characters with mass greater than 100
const greater100Characters = characters.filter(character => (character.mass > 100));
// console.log(greater100Characters);
//2. Get characters with height less than 200
const shorterCharacters = characters.filter(character => (character.height < 200));
// console.log(shorterCharacters);
//3. Get all male characters
const maleCharacters = characters.filter(character => (character.gender === 'male'));
// console.log(maleCharacters);
//4. Get all female characters
const femaleCharacters = characters.filter(character => (character.gender === 'female'));
// console.log(femaleCharacters);
//***SORT***
//1. Sort by mass
const byMass = characters.sort((a, b) => (
a.mass - b.mass
));
// console.log(byMass);
//2. Sort by height
const byHeight = characters.sort((a, b) => (
a.height - b.height
));
// console.log(byHeight);
//3. Sort by name
const byName = characters.sort((a, b) => {
if (a.name < b.name) {
return -1;
} else {
return 1;
}
});
// console.log(byName);
//4. Sort by gender
const byGender = characters.sort((a, b) => {
if (a.gender === 'female') {
return -1;
} else {
return 1
}
});
// console.log(byGender);
//***EVERY***
//1. Does every character have blue eyes?
const allBlueEyes = characters.every(character => (
character.eye_color === 'blue'
));
// console.log(allBlueEyes);
//2. Does every character have mass more than 40?
const allMassMoreThan40 = characters.every(character => (
character.mass > 40
));
// console.log(allMassMoreThan40);
//3. Is every character shorter than 200?
const allShorterThan200 = characters.every(character => (
character.height < 200
));
// console.log(allShorterThan200);
//4. Is every character male?
const allMale = characters.every(character => (
character.gender === 'male'
));
// console.log(allMale);
//***SOME***
//1. Is there at least one male character?
const oneMaleCharacter = characters.some(character => (
character.gender === 'male'
));
// console.log(oneMaleCharacter);
//2. Is there at least one character with blue eyes?
const oneCharacterBlueEyes = characters.some(character => (
character.eye_color === 'blue'
));
// console.log(oneCharacterBlueEyes);
//3. Is there at least one character taller than 210?
const oneCharacterTallerThan210 = characters.some(character => (
character.height > 210
));
// console.log(oneCharacterTallerThan210);
//4. Is there at least one character that has mass less than 50?
const oneCharacterMassLessThan50 = characters.some(character => (
character.mass < 50
));
// console.log(oneCharacterMassLessThan50);