Advertisement
Guest User

Cycling through arrays meme JS

a guest
Nov 13th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // our array (unsorted)
  2. let array = [1,3,5,2,4,7,9,6,8,11,15,14,13,12];
  3. // for loop
  4. for(let i = 0; i < array.length; i++)
  5. {
  6.  
  7. }
  8. // reverse for loop
  9. for(let i = array.length - 1; i >= 0; i--)
  10. {
  11.  
  12. }
  13. // for loop (JS Edition)
  14. for(let i in array)
  15. {
  16.  
  17. }
  18. // better for loop (JS Edition)
  19. for(let obj of array)
  20. {
  21.  
  22. }
  23. // simpler for loop (JS Edition)
  24. array.forEach(obj=>{
  25.  
  26. });
  27. // static function (Function)
  28. function GetObject(x)
  29. {
  30.   if(!this.selectedObject || this.selectedObject != x) this.selectedObject = x;
  31.   if(!this.selectedIndex) this.selectedIndex = 0;
  32.   if(x[this.selectedIndex]) return x[this.selectedIndex++];
  33.   else
  34.   {
  35.     this.selectedObject = null;
  36.     this.selectedIndex = 0;
  37.     return undefined;
  38.   }
  39. }
  40. // static function (Usage)
  41. let obj;
  42. while(obj = GetObject(array)) {  }
  43. // recursive function (LMAO)
  44. function RecurseObject(array, i)
  45. {
  46.   let obj = array[i];
  47.   if(array[++i])
  48.     RecurseObject(i);
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement