Guest User

Untitled

a guest
May 25th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. // you can pass in `scratchData` to test out `findByid`
  12. // your function
  13. var scratchData = [
  14. {id: 22, foo: 'bar'},
  15. {id: 28, foo: 'bizz'},
  16. {id: 19, foo: 'bazz'}
  17. ];
  18.  
  19. function findById(items, idNum) {
  20. var findIdNum = [];
  21. var searchedItem = items.find(function(item){
  22. if(item.id == idNum) {
  23. return true;
  24. } else {
  25. return false;
  26. }
  27. })
  28. return searchedItem
  29. }
  30.  
  31.  
  32. //
  33.  
  34. function testIt() {
  35. var testData = [
  36. {id: 1, foo: 'bar'},
  37. {id: 2, foo: 'bizz'},
  38. {id: 3, bang: 'boo'}
  39. ];
  40. var result = findById(testData, 3);
  41. if (!(result && result !== null && typeof result === 'object')) {
  42. console.error('`findById` must return an object');
  43. return
  44. }
  45. if (result.id !== 3) {
  46. console.error(
  47. 'Asked for item with id of `3` but got back one with id of ' +
  48. result.id
  49. );
  50. return
  51. }
  52. if (result.bang !== 'boo') {
  53. console.error('Expected all key/value pairs from target object to be returned');
  54. return
  55. }
  56. console.log('SUCCESS: `findByid` is working');
  57. }
  58.  
  59. testIt();
  60. </script>
  61.  
  62.  
  63.  
  64. <script id="jsbin-source-javascript" type="text/javascript">// you can pass in `scratchData` to test out `findByid`
  65. // your function
  66. var scratchData = [
  67. {id: 22, foo: 'bar'},
  68. {id: 28, foo: 'bizz'},
  69. {id: 19, foo: 'bazz'}
  70. ];
  71.  
  72. function findById(items, idNum) {
  73. var findIdNum = [];
  74. var searchedItem = items.find(function(item){
  75. if(item.id == idNum) {
  76. return true;
  77. } else {
  78. return false;
  79. }
  80. })
  81. return searchedItem
  82. }
  83.  
  84.  
  85. //
  86.  
  87. function testIt() {
  88. var testData = [
  89. {id: 1, foo: 'bar'},
  90. {id: 2, foo: 'bizz'},
  91. {id: 3, bang: 'boo'}
  92. ];
  93. var result = findById(testData, 3);
  94. if (!(result && result !== null && typeof result === 'object')) {
  95. console.error('`findById` must return an object');
  96. return
  97. }
  98. if (result.id !== 3) {
  99. console.error(
  100. 'Asked for item with id of `3` but got back one with id of ' +
  101. result.id
  102. );
  103. return
  104. }
  105. if (result.bang !== 'boo') {
  106. console.error('Expected all key/value pairs from target object to be returned');
  107. return
  108. }
  109. console.log('SUCCESS: `findByid` is working');
  110. }
  111.  
  112. testIt();</script></body>
  113. </html>
Add Comment
Please, Sign In to add comment