-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.js
44 lines (32 loc) · 1.38 KB
/
solution.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
// 33: array - `Array.prototype.findIndex` (solution)
// To do: make all tests pass, leave the assert lines unchanged!
describe('`Array.prototype.findIndex` makes finding items in arrays easier', () => {
it('takes a compare function, returns the index where it returned true', function() {
const foundAt = [false, true].findIndex( ( item ) => item === true );
assert.equal(foundAt, 1);
});
it('returns the first position it was found at', function() {
const foundAt = [0, 1, 1, 1].findIndex(item => item === 1);
assert.equal(foundAt, 1);
});
it('returns `-1` when nothing was found', function() {
const foundAt = [1, 2, 3].findIndex(item => item > 3);
assert.equal(foundAt, -1);
});
it('the findIndex callback gets the item, index and array as arguments', function() {
const three = 3;
const containsThree = arr => arr.indexOf(three) > -1;
function theSecondThree(item, index, arr) {
const preceedingItems = arr.slice(0, index);
return containsThree(preceedingItems);
}
const foundAt = [1, 1, 2, 3, 3, 3].findIndex(theSecondThree);
assert.equal(foundAt, 4);
});
it('combined with destructuring complex compares become short', function() {
const bob = {name: 'Bob'};
const alice = {name: 'Alice'};
const foundAt = [bob, alice].findIndex(({name:{length}}) => length > 3);
assert.equal(foundAt, 1);
});
});