-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenges-11.test.js
199 lines (155 loc) · 7.17 KB
/
challenges-11.test.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
'use strict';
/* ------------------------------------------------------------------------------------------------
CHALLENGE 1 - Review
Write a function named transformToLis that, given an object, returns an array of the key value pairs as html list items.
For example:
{
name: 'bob',
age: 32
}
Becomes:
[
<li>name: bob</li>,
<li>age: 32</li>
]
------------------------------------------------------------------------------------------------ */
function transformToLis(obj){
return Object.entries(obj).map(entry => `<li>${entry[0]}: ${entry[1]}</li>`);
}
/* ------------------------------------------------------------------------------------------------
CHALLENGE 2
Write a function named count that, given an integer and an array of arrays, uses either filter, map, or reduce to count the amount of times the integer is present in the array of arrays.
Note: You might need to use the same method more than once.
For example, count(5, [[1, 3, 5, 7, 9], [5, 5, 5], [1, 2, 3]]) returns 4.
------------------------------------------------------------------------------------------------ */
const count = (target, input) => input.reduce((a,b) => a.concat(b), []).filter(item => item === target).length;
/* ------------------------------------------------------------------------------------------------
CHALLENGE 3
Write a function that, given an array of integer arrays as input, calculates the total sum of all the elements in the array.
You may want to use filter, map, or reduce for this problem, but are not required to. You may need to use the same method more than once.
For example, [[1, 2, 3, 4, 5], [6, 7, 2, 4, 5, 7], [9, 2, 3, 6,]] returns 66.
------------------------------------------------------------------------------------------------ */
const totalSum = (input) => input.reduce((a,b) => a.concat(b), []).reduce((a,b) => a + b, 0);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 4
Write a function named divisibleByFiveTwoToThePower that accepts an array of arrays as input.
This function should first remove any elements that are not numbers or are not divisible by five.
This function should then raise 2 to the power of the resulting numbers, returning an array of arrays.
For example, [ [0,2,5,4], [2,4,10], [] ] should return [ [1, 32], [1024], [] ].
------------------------------------------------------------------------------------------------ */
// TODO
/* ------------------------------------------------------------------------------------------------
CHALLENGE 5
Write a function named findMaleAndFemale that, given the Star Wars data, below,
returns the names of the characters whose gender is either male or female.
The names should be combined into a single string with each character name separated by "and".
For example, "Darth Vader and Luke Skywalker".
------------------------------------------------------------------------------------------------ */
let starWarsData = [{
name: 'Luke Skywalker',
height: '172',
mass: '77',
hair_color: 'blond',
skin_color: 'fair',
eye_color: 'blue',
birth_year: '19BBY',
gender: 'male',
},
{
name: 'C-3PO',
height: '167',
mass: '75',
hair_color: 'n/a',
skin_color: 'gold',
eye_color: 'yellow',
birth_year: '112BBY',
gender: 'n/a'
},
{
name: 'R2-D2',
height: '96',
mass: '32',
hair_color: 'n/a',
skin_color: 'white, blue',
eye_color: 'red',
birth_year: '33BBY',
gender: 'n/a'
},
{
name: 'Darth Vader',
height: '202',
mass: '136',
hair_color: 'none',
skin_color: 'white',
eye_color: 'yellow',
birth_year: '41.9BBY',
gender: 'male'
},
{
name: 'Leia Organa',
height: '150',
mass: '49',
hair_color: 'brown',
skin_color: 'light',
eye_color: 'brown',
birth_year: '19BBY',
gender: 'female'
}];
let findMaleAndFemale = (data) => data.filter(char => ['female','male'].includes(char.gender)).reduce((a,b,c) => c === 0 ? `${b.name}` : `${a} and ${b.name}`, '');
/* ------------------------------------------------------------------------------------------------
CHALLENGE 6
Write a function named findShortest that, given the Star Wars data from Challenge 6, uses any combination of filter, map and reduce to return the name of the shortest character.
------------------------------------------------------------------------------------------------ */
let findShortest = (data) => data.reduce((a,b) => a.height < b.height ? b : a).name;
/* ------------------------------------------------------------------------------------------------
TESTS
All the code below will verify that your functions are working to solve the challenges.
DO NOT CHANGE any of the below code.
Run your tests from the console: jest challenges-10.test.js
------------------------------------------------------------------------------------------------ */
describe('Testing challenge 1', () => {
test('It should return a list of key value pairs inside of li tags', () => {
expect(transformToLis({name: 'bob', age: 32})[0]).toStrictEqual(`<li>name: bob</li>`);
expect(transformToLis({name: 'bob', age: 32})[1]).toStrictEqual(`<li>age: 32</li>`);
expect(transformToLis({})).toStrictEqual([]);
});
});
describe('Testing challenge 2', () => {
test('It should return the number of times the input is in the nested arrays', () => {
expect(count(5, [[1, 3, 5, 7, 9], [5, 5, 5], [1, 2, 3]])).toStrictEqual(4);
expect(count(3, [[1, 3, 5, 7, 9], [5, 5, 5], [1, 2, 3]])).toStrictEqual(2);
expect(count(12, [[1, 3, 5, 7, 9], [5, 5, 5], [1, 2, 3]])).toStrictEqual(0);
});
test('It should work on empty arrays', () => {
expect(count(5, [[1, 3, 5, 7, 9], [], [5, 5, 5], [1, 2, 3], []])).toStrictEqual(4);
expect(count(5, [])).toStrictEqual(0);
});
});
describe('Testing challenge 3', () => {
test('It should add all the numbers in the arrays', () => {
const nums = [[1, 2, 3, 4, 5], [6, 7, 2, 4, 5, 7], [9, 2, 3, 6,]];
expect(totalSum(nums)).toStrictEqual(66);
});
});
describe.skip('Testing challenge 4', () => {
test('It should return numbers divisible by five, then raise two to the power of the resulting numbers', () => {
expect(divisibleByFiveTwoToThePower([[10, 20, 5, 4], [5, 6, 7, 9], [1, 10, 3]])).toStrictEqual([[1024, 1048576, 32], [32], [1024]]);
});
test('It should return an empty array if none of the numbers are divisible by five', () => {
expect(divisibleByFiveTwoToThePower([[1, 2, 3], [5, 10, 15]])).toStrictEqual([[], [32, 1024, 32768]]);
});
test('It should return an empty array if the values are not numbers', () => {
expect(divisibleByFiveTwoToThePower([['one', 'two', 'five'], ['5', '10', '15'], [5]])).toStrictEqual([[], [], [32]]);
});
});
describe('Testing challenge 5', () => {
test('It should return only characters that are male or female', () => {
expect(findMaleAndFemale(starWarsData)).toStrictEqual('Luke Skywalker and Darth Vader and Leia Organa');
expect(findMaleAndFemale([{ name: 'person', gender: 'female' }, { gender: 'lol' }, { name: 'persontwo', gender: 'male' }])).toStrictEqual('person and persontwo');
});
});
describe('Testing challenge 6', () => {
test('It should return the name of the shortest character', () => {
expect(findShortest(starWarsData)).toStrictEqual('R2-D2');
});
});