Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. // return true if an item exists in list
  2. // and false if it doesn't
  3. var fruits = ["Banana", "Orange", "Apple", "Mango"];
  4. var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];
  5.  
  6. function checkItemInList(item, list) {
  7. return list.includes(item);
  8. }
  9.  
  10. console.log('Is Mango in Fruit List:', checkItemInList('Mango', fruits)); // true
  11. console.log('Is Avocado in Fruit List:', checkItemInList('Avocado', fruits)); // false
  12.  
  13. console.log('Is 14 in numbers list:', checkItemInList(14, numbers)); // true
  14. console.log('Is 15 in numbers list:', checkItemInList(15, numbers)); // false
  15.  
  16. // **********************************************************************************
  17. // - In the case the run time of checkItemInList is O(n)
  18. // - n being the number of item in the array list
  19. // - Big O Notaion favor the worse case scenaior: the item we are looking for
  20. // - is the last item in the list array
  21. // **********************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement