-
Notifications
You must be signed in to change notification settings - Fork 253
Expand file tree
/
Copy pathsolutions.txt
More file actions
156 lines (121 loc) · 4.09 KB
/
solutions.txt
File metadata and controls
156 lines (121 loc) · 4.09 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
import { useEffect, useState } from "react";
export default function App() {
const [characters, setCharacters] = useState([
{
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"
}
]);
useEffect(() => {
// Get the total mass of all characters
const totalMass = characters.reduce((acc, character) => acc + Number(character.mass),0);
console.log(totalMass);
// Get the total height of all characters
const totalHeight = characters.reduce((acc,ch)=>acc+Number(ch.height),0)
console.log(totalHeight)
// Get the total number of characters in all the character names
const characterLength = characters.reduce((acc,ch)=> acc+ ch.name.split(' ')[0].length + ch.name.split(' ')[1].length,0)
console.log(characterLength)
// Get the total number of characters by eye color (hint. a map of eye color to count)
const eyeColorCount = characters.reduce((acc, character) => {
const eyeColor = character.eye_color;
acc[eyeColor] = (acc[eyeColor] || 0) + 1;
return acc;
}, {});
console.log(eyeColorCount);
// Get characters with mass greater than 100
const character100 = characters.filter((ch)=>ch.mass > 100)
console.log(character100)
// Get characters with height less than 200
const character200 = characters.filter((ch)=>ch.height<200)
console.log(character200)
// Get all male characters
const characterMale = characters.filter((ch)=>ch.gender === 'male')
console.log(characterMale)
// Get all female characters
const characterFemale = characters.filter((ch)=>ch.gender === 'female')
console.log(characterFemale)
// sort by name
const sortName = characters.sort((a,b)=>{
const NameA = a.name.toUpperCase()
const NameB = b.name.toUpperCase()
if(NameA<NameB){return -1}
if(NameA>NameB){return 1}
return 0;
})
console.log(sortName)
// sort by mass
const sortMass = characters.sort((a,b)=>a.mass - b.mass)
console.log(sortMass)
// sort by height
const sortHeight = characters.sort((a,b)=>a.height - b.height)
console.log(sortHeight)
// sort by gender
const sortGender =characters.sort((a,b)=>{
const genderA = a.gender.toUpperCase()
const genderB = b.gender.toUpperCase()
if(genderA<genderB){return -1}
if(genderA>genderB){return 1}
return 0
})
console.log(sortGender)
// does every character has blue eyes
const blueEyes = characters.every((ch)=>ch.eye_color==='blue')
console.log(blueEyes)
// Does every character have mass more than 40?
const mass40 = characters.every((ch)=>ch.mass> 40)
console.log(mass40)
// Is every character shorter than 200?
const height200 = characters.every((ch)=>ch.height>200)
console.log(height200)
// Is every character male?
const genderMale = characters.every((ch)=>ch.gender ==='male')
console.log(genderMale)
// Is there at least one male character?
const someMale = characters.some((ch)=>ch.gender==='male')
console.log(someMale)
// Is there at least one character with blue eyes?
const someBlueEyes = characters.some((ch)=>ch.eye_color==='blue')
console.log(someBlueEyes)
//Is there at least one character taller than 200?
const someTaller200 = characters.some((ch)=>ch.height>200)
console.log(someTaller200)
// Is there at least one character that has mass less than 50?
const someMass50 = characters.some((ch)=>ch.mass<50)
console.log(someMass50)
}, [characters]);
return (
<>
{/* Get an array of all names */}
{characters.map((ch) => (<h1 key={ch.name}>{ch.name}</h1>))}
{/* Get an array of all heights */}
{characters.map((ch) => (<h1 key={ch.name}>{ch.height}</h1>))}
{/* Get an array of objects with just name and height properties */}
{characters.map((ch) => (<h1 key={ch.name}>{ch.name} {ch.height}</h1>))}
{/* Get an array of all first names */}
{characters.map((ch) => (<h1 key={ch.name}>{ch.name.split(" ")[0]}</h1>))}
</>);}