Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. // you can pass in `scratchData` to test out `findByid`
  2. // your function
  3. const scratchData = [
  4. { id: 22, foo: 'bar' },
  5. { id: 28, foo: 'bizz' },
  6. { id: 19, foo: 'bazz' },
  7. ];
  8.  
  9. function findById(items, idNum) {
  10. return items.find(item => item.id === idNum);
  11.  
  12. }
  13.  
  14. //
  15.  
  16. function testIt() {
  17. const testData = [
  18. { id: 1, foo: 'bar' },
  19. { id: 2, foo: 'bizz' },
  20. { id: 3, bang: 'boo' },
  21. ];
  22. const result = findById(testData, 3);
  23. if (!(result && result !== null && typeof result === 'object')) {
  24. console.error('`findById` must return an object');
  25. return;
  26. }
  27. if (result.id !== 3) {
  28. console.error(
  29. 'Asked for item with id of `3` but got back one with id of ' + result.id
  30. );
  31. return;
  32. }
  33. if (result.bang !== 'boo') {
  34. console.error(
  35. 'Expected all key/value pairs from target object to be returned'
  36. );
  37. return;
  38. }
  39. console.log('SUCCESS: `findByid` is working');
  40. }
  41.  
  42. testIt();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement