Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. // 33: array - `Array.prototype.findIndex`
  2. // To do: make all tests pass, leave the assert lines unchanged!
  3. // Follow the hints of the failure messages!
  4.  
  5. describe('`Array.prototype.findIndex` makes finding items in arrays easier', () => {
  6. it('takes a compare function, returns the index where it returned true', function() {
  7. function checkTrue (_) {
  8. return _ === true
  9. }
  10. const foundAt = [false, true].findIndex(checkTrue);
  11. assert.equal(foundAt, 1);
  12. });
  13. it('returns the first position it was found at', function() {
  14. const foundAt = [0, 1, 1, 1].findIndex(item => item == 1);
  15. assert.equal(foundAt, 1);
  16. });
  17. it('returns `-1` when nothing was found', function() {
  18. const foundAt = [1, 1, 1].findIndex(item => item > 1);
  19. assert.equal(foundAt, -1);
  20. });
  21. it('the findIndex callback gets the item, index and array as arguments', function() {
  22. const myArray = [1, 1, 2, 3, 4, 3]
  23. const foundAt = myArray.findIndex( _ => _ === 4 );
  24. assert.equal(foundAt, 4);
  25. });
  26. it('combined with destructuring complex compares become short', function() {
  27. const bob = {name: 'Bob'};
  28. const alice = {name: 'Alice'};
  29. const foundAt = [bob, alice].findIndex(({name:{length}}) => length > 3);
  30. assert.equal(foundAt, 1);
  31. });
  32. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement