example.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. var bounds = require('../search-bounds')
  2. //Create an array
  3. var array = [1, 2, 3, 3, 3, 5, 6, 10, 11, 13, 50, 1000, 2200]
  4. //Print all elements in array contained in the interval [3, 50)
  5. console.log(
  6. array.slice(
  7. bounds.ge(array, 3),
  8. bounds.lt(array, 50)))
  9. //Test if array contains the element 4
  10. console.log('indexOf(6)=', bounds.eq(array, 6))
  11. console.log('indexOf(4)=', bounds.eq(array, 4))
  12. //Find the element immediately after 13
  13. console.log('successor of 13 =', array[bounds.gt(array, 13)])
  14. //Find the element in the array before 4
  15. console.log('predecessor of 4 =', array[bounds.lt(array, 4)])
  16. //Create an array of objects
  17. var creatures = [
  18. { legs: 8, name: 'spider' },
  19. { legs: 4, name: 'mouse' },
  20. { legs: 4, name: 'cat' },
  21. { legs: 2, name: 'Ben Franklin' },
  22. { legs: 4, name: 'table', isCreature: false },
  23. { legs: 100, name: 'centipede' },
  24. { legs: 4, name: 'dog' },
  25. { legs: 6, name: 'ant' }
  26. ]
  27. //Sort the array by number of legs
  28. function byLegs(a,b) { return a.legs - b.legs }
  29. creatures.sort(byLegs)
  30. //Find the next creature with more than 4 legs
  31. console.log('What has more than 4 legs? Answer:', creatures[bounds.gt(creatures, {legs:4}, byLegs)])