Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.08 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="description" content="Loops">
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width">
  7. <title>JS Bin</title>
  8. </head>
  9. <body>
  10.  
  11. <script id="jsbin-javascript">
  12. /*
  13. LOOPS.
  14.  
  15. In JavaScript Loops are especially helpful and invaluable in the sense that they allow us
  16. to run a block of code however many times we'd like. Also Loops allow us to iterate over
  17. collections of data & pull values from them. Loops check a condition. If that condition
  18. returns true,a block of code will run. Then the condition will be checked again and if it
  19. still returns true, the code block will run again. The Loop repeats this process until the
  20. condition returns false.
  21. Here are 3 common types of Loops:
  22. */
  23. //For Loops:
  24. for(var j = 1; j <= 9; j++) {
  25. console.log(j);// Logs 1-9, loop ends when J is = to 9
  26. };
  27. for(var y = 3; y > 0; y--) {
  28. console.log(y);// Logs 3-1, loop ends when y is > but not = to 0
  29. }
  30. //For Loops w/Arrays: logging a movie list
  31. var movieList = [ 'Eternal Sunshine of a Spotless Mind', 'Oldboy',
  32. 'Hanna', 'Vanilla Sky', "Pan's Labyrinth",
  33. 'Spirited Away', 'Tron'];
  34. for(let i = 0; i < movieList.length; i++) {
  35. console.log(movieList[i]);// Logs movie titles to console
  36. };
  37. for(var i = movieList.length; i >= 0; i--) {
  38. console.log(movieList[i]);// Logs titles in reverse
  39. };
  40. //While Loops: logging same movie list in a different way
  41. var list1 = "The Movie List is:\n";
  42. var list2 = "";
  43. var x = 1;
  44. while( x < movieList.length) {
  45. list2 += movieList[x]+"\n";
  46. x++;
  47. var list3 = list1 + list2;
  48. };
  49. console.log(list3);//Logs titles with a line break after each
  50. //For-In Loops: looping over objects
  51. var xxSongList = {
  52. song1: "lips",
  53. song2: "Infinity",
  54. song3: "Sunset",
  55. song4: "Islands",
  56. song5: "Fiction",
  57. song6: "Chained",
  58. song7: "Dangerous"
  59. };
  60. for(var q in xxSongList) {
  61. console.log(xxSongList[q]);
  62. }
  63. //For-In Loops: looping over objects backwards
  64. function reverseObj(obj) {
  65. var newArr = [];
  66. for(var key in obj) {
  67. if(obj.hasOwnProperty(key)) {
  68. newArr.push(obj[key]);
  69. }
  70. }
  71. for(var v = newArr.length-1; v >=0; v--){
  72. console.log(newArr[v]);
  73. }
  74. }
  75. reverseObj(xxSongList);// Logs The XX song list in reverse
  76. </script>
  77.  
  78.  
  79.  
  80. <script id="jsbin-source-javascript" type="text/javascript">/*
  81. LOOPS.
  82.  
  83. In JavaScript Loops are especially helpful and invaluable in the sense that they allow us
  84. to run a block of code however many times we'd like. Also Loops allow us to iterate over
  85. collections of data & pull values from them. Loops check a condition. If that condition
  86. returns true,a block of code will run. Then the condition will be checked again and if it
  87. still returns true, the code block will run again. The Loop repeats this process until the
  88. condition returns false.
  89. Here are 3 common types of Loops:
  90. */
  91. //For Loops:
  92. for(var j = 1; j <= 9; j++) {
  93. console.log(j);// Logs 1-9, loop ends when J is = to 9
  94. };
  95. for(var y = 3; y > 0; y--) {
  96. console.log(y);// Logs 3-1, loop ends when y is > but not = to 0
  97. }
  98. //For Loops w/Arrays: logging a movie list
  99. var movieList = [ 'Eternal Sunshine of a Spotless Mind', 'Oldboy',
  100. 'Hanna', 'Vanilla Sky', "Pan's Labyrinth",
  101. 'Spirited Away', 'Tron'];
  102. for(let i = 0; i < movieList.length; i++) {
  103. console.log(movieList[i]);// Logs movie titles to console
  104. };
  105. for(var i = movieList.length; i >= 0; i--) {
  106. console.log(movieList[i]);// Logs titles in reverse
  107. };
  108. //While Loops: logging same movie list in a different way
  109. var list1 = "The Movie List is:\n";
  110. var list2 = "";
  111. var x = 1;
  112. while( x < movieList.length) {
  113. list2 += movieList[x]+"\n";
  114. x++;
  115. var list3 = list1 + list2;
  116. };
  117. console.log(list3);//Logs titles with a line break after each
  118. //For-In Loops: looping over objects
  119. var xxSongList = {
  120. song1: "lips",
  121. song2: "Infinity",
  122. song3: "Sunset",
  123. song4: "Islands",
  124. song5: "Fiction",
  125. song6: "Chained",
  126. song7: "Dangerous"
  127. };
  128. for(var q in xxSongList) {
  129. console.log(xxSongList[q]);
  130. }
  131. //For-In Loops: looping over objects backwards
  132. function reverseObj(obj) {
  133. var newArr = [];
  134. for(var key in obj) {
  135. if(obj.hasOwnProperty(key)) {
  136. newArr.push(obj[key]);
  137. }
  138. }
  139. for(var v = newArr.length-1; v >=0; v--){
  140. console.log(newArr[v]);
  141. }
  142. }
  143. reverseObj(xxSongList);// Logs The XX song list in reverse</script></body>
  144. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement