Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 33: array - `Array.prototype.findIndex`
- // To do: make all tests pass, leave the assert lines unchanged!
- // Follow the hints of the failure messages!
- describe('`Array.prototype.findIndex` makes finding items in arrays easier', () => {
- it('takes a compare function, returns the index where it returned true', function() {
- function checkTrue (_) {
- return _ === true
- }
- const foundAt = [false, true].findIndex(checkTrue);
- 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, 1, 1].findIndex(item => item > 1);
- assert.equal(foundAt, -1);
- });
- it('the findIndex callback gets the item, index and array as arguments', function() {
- const myArray = [1, 1, 2, 3, 4, 3]
- const foundAt = myArray.findIndex( _ => _ === 4 );
- 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);
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement