Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 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. /* 1. Loops:
  13. * Loops are a way of iterating over data and repeating yourself. They help us pull out
  14. * individual elements and work with them one by one. Let's look at for loops,
  15. * while loops, and for-in loops.
  16. */
  17.  
  18. var bird = "Eagle";
  19.  
  20. for(var i = 0; i < bird.length; i++) {
  21. console.log(bird[i]);
  22. }
  23. /* Prints "E", "a", "g", "'l", "e". It iterates over the entire string, and returns
  24. * the element at each position.
  25. */
  26.  
  27. //Let's go backwards!
  28. for (var i = bird.length-1; i > -1; i--) {
  29. console.log(bird[i]);
  30. }
  31. /* Prints "e", "l", "g", "a", "E". The loop iterates backward over the string and
  32. * returns the elements as it encounter them.
  33. */
  34.  
  35. //While loops are similar to for loops, but syntactically different.
  36.  
  37. var ducks = 1;
  38. while(ducks < 10) {
  39. console.log(ducks);
  40. ducks++;
  41. }
  42. // Prints the numbers 1-9 as it iterates over the loop.
  43.  
  44. /* We can also loop over arrays and objects.
  45. */
  46.  
  47. var uhRay = ["duck", "tuck", "goose"];
  48. for (var i = 0; i < uhRay.length; i++) {
  49. console.log(uhRay[i]);
  50. }
  51. //This logs "duck", "tuck", and "goose".
  52.  
  53. //backwards!!!!
  54. var rayUh = ["it's", "a", "trap"];
  55.  
  56. for (var i = rayUh.length-1; i > -1; i--) {
  57. console.log(rayUh[i]);
  58. }
  59. //Prints our elements in reverse order.
  60.  
  61. //for-in loops allow us to iterate through an object.
  62.  
  63. var skyrimSoldier ={
  64. knee: "Arrow",
  65. fear: "That Dragons have returned!"
  66. };
  67.  
  68. for (var key in skyrimSoldier) {
  69. console.log(skyrimSoldier[key]);
  70. }
  71. //This prints out the values but what about printing the keys?
  72.  
  73. var jar = {
  74. pickles: "green",
  75. eggs: "gross"
  76. };
  77.  
  78. for (var key in jar) {
  79. console.log(key);
  80. }
  81.  
  82. //This prints the key values.
  83. </script>
  84.  
  85.  
  86.  
  87. <script id="jsbin-source-javascript" type="text/javascript">/* 1. Loops:
  88. * Loops are a way of iterating over data and repeating yourself. They help us pull out
  89. * individual elements and work with them one by one. Let's look at for loops,
  90. * while loops, and for-in loops.
  91. */
  92.  
  93. var bird = "Eagle";
  94.  
  95. for(var i = 0; i < bird.length; i++) {
  96. console.log(bird[i]);
  97. }
  98. /* Prints "E", "a", "g", "'l", "e". It iterates over the entire string, and returns
  99. * the element at each position.
  100. */
  101.  
  102. //Let's go backwards!
  103. for (var i = bird.length-1; i > -1; i--) {
  104. console.log(bird[i]);
  105. }
  106. /* Prints "e", "l", "g", "a", "E". The loop iterates backward over the string and
  107. * returns the elements as it encounter them.
  108. */
  109.  
  110. //While loops are similar to for loops, but syntactically different.
  111.  
  112. var ducks = 1;
  113. while(ducks < 10) {
  114. console.log(ducks);
  115. ducks++;
  116. }
  117. // Prints the numbers 1-9 as it iterates over the loop.
  118.  
  119. /* We can also loop over arrays and objects.
  120. */
  121.  
  122. var uhRay = ["duck", "tuck", "goose"];
  123. for (var i = 0; i < uhRay.length; i++) {
  124. console.log(uhRay[i]);
  125. }
  126. //This logs "duck", "tuck", and "goose".
  127.  
  128. //backwards!!!!
  129. var rayUh = ["it's", "a", "trap"];
  130.  
  131. for (var i = rayUh.length-1; i > -1; i--) {
  132. console.log(rayUh[i]);
  133. }
  134. //Prints our elements in reverse order.
  135.  
  136. //for-in loops allow us to iterate through an object.
  137.  
  138. var skyrimSoldier ={
  139. knee: "Arrow",
  140. fear: "That Dragons have returned!"
  141. };
  142.  
  143. for (var key in skyrimSoldier) {
  144. console.log(skyrimSoldier[key]);
  145. }
  146. //This prints out the values but what about printing the keys?
  147.  
  148. var jar = {
  149. pickles: "green",
  150. eggs: "gross"
  151. };
  152.  
  153. for (var key in jar) {
  154. console.log(key);
  155. }
  156.  
  157. //This prints the key values.</script></body>
  158. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement