Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. // -----Loops----- //
  2.  
  3. /* For loops are great tools for iterating over individual elements within an array
  4. to work with them. A for loop has three sections within its proceeding parentheses: an
  5. initialization, conditional statement, and a final expression. The initialization phase
  6. usually declares a variable. The conditional statement phase tells the loop when to stop,
  7. and the final expression performs an action at the end, usually incrementing or
  8. decrementing a counter: */
  9.  
  10. "use strict";
  11.  
  12. var arr = ["What is love?", "I love you", "I hate you", 24, "Baby, don't hurt me"];
  13.  
  14. for (var i = 0; i < arr.length; i++) {
  15. if (i === 0 || i === 4) {
  16. console.log(arr[i]);
  17. }
  18. }
  19.  
  20. /* It is possible to loop through the array in more than one direction: */
  21.  
  22. for (var i = arr.length; i > 0; i--) {
  23. if (i === 2) {
  24. console.log(arr[i]);
  25. }
  26. }
  27.  
  28. /* a while loop is similar to a for loop, except a while loop only contains a conditional
  29. statement within its parentheses. It will require a counter of some sort to tell the loop
  30. when to stop. This counter is easy to forget but VERY important; executing a while loop without
  31. it will cause an infinite loop and possibly crash the program. */
  32.  
  33. var n = 0;
  34.  
  35. while (n < 3) {
  36. console.log("Beatlejuice");
  37. n += 1; // super important to remember
  38. }
  39.  
  40. /* A for in loop does for objects what the for loop does for an array--it iterates over the
  41. object's enumerable properties and performs an action at every stop. */
  42.  
  43. var obj = {
  44. a: 1,
  45. b: 2,
  46. c: 3
  47. };
  48.  
  49. for (var key in obj) {
  50. console.log(key + ": " + obj[key]);
  51. }
  52.  
  53. /* Since objects are innately unsorted and looping through in a particular order can't be guaranteed,
  54. to get closer to looping in the order you wish, some external arranging must first take place.
  55. For example, using a regular for in loop may iterate through the key/values in the order they
  56. were assigned. To loop backwards, you could extract the keys and place them into an array,
  57. reverse the order of the array, and then use a for loop to iterate through the array and return
  58. a new object: */
  59.  
  60. function reverseObj(object) {
  61.  
  62. var newObj = {};
  63. var arr = Object.keys(object);
  64.  
  65. arr.reverse(); // could also skip this line and iterate backwards in the for loop instead
  66.  
  67. for (var i = 0; i < arr.length; i++) {
  68. newObj[arr[i]] = obj[arr[i]];
  69. // console.log(newObj); Uncomment me and you will see it insert the key/values in reverse
  70. }
  71. return newObj;
  72. }
  73.  
  74. reverseObj(obj);
  75.  
  76. /* Depending on which browser or program runs this code, it may still log it in the console
  77. in alphabetical order, but logging within the for loop displays the correct results. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement