Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="description" content="Whats are loops?">
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width">
  7. <title>Loops</title>
  8. </head>
  9. <body>
  10.  
  11. <script id="jsbin-javascript">
  12. /* Loops
  13. There are many different kinds of loops, but they all essentially do the same thing: they repeat an action some number of times.
  14. The various loop mechanisms offer different ways to determine the start and end points of the loop.
  15. */
  16.  
  17. // 1. for loop
  18. for(var i = 0; i < 10; i++){ //===> loops from 0 to 10//
  19. console.log(i); //===> prints all numbers in the loop//
  20. }
  21.  
  22. for(var j = 10; j > 0; j--){
  23. console.log(j); //===> prints bacwards from 10 to 1//
  24. }
  25.  
  26. // Loop through arrays
  27. function loopArray(myArray){
  28. for(var a = 0; a < myArray.length; a++){
  29. console.log(myArray[a]);
  30. }
  31. }
  32. loopArray(["david", 28, "pizza"]);
  33.  
  34. function loopReverseArray(myArray){
  35. for(var b = myArray.length -1; b >= 0; b--){
  36. console.log(myArray[b]);
  37. }
  38. }
  39. loopReverseArray(["david", 28, "pizza"]);
  40.  
  41. // 2. Loop through objects using for..in
  42. function objectKeys(myObject) {
  43. for (var prop in myObject) {
  44. console.log(myObject[prop]);
  45. }
  46. }
  47. objectKeys({name: "gabby", age: 27, food: "chips"})
  48.  
  49.  
  50.  
  51. // 3. while loop
  52. var w = 7;
  53. while (w < 10) {
  54. console.log(w); //===> 7, 8, 9
  55. w++;
  56. }
  57. //the code in the loop will run as long as the variable (w) is less than 10
  58. </script>
  59.  
  60.  
  61.  
  62. <script id="jsbin-source-javascript" type="text/javascript">/* Loops
  63. There are many different kinds of loops, but they all essentially do the same thing: they repeat an action some number of times.
  64. The various loop mechanisms offer different ways to determine the start and end points of the loop.
  65. */
  66.  
  67. // 1. for loop
  68. for(var i = 0; i < 10; i++){ //===> loops from 0 to 10//
  69. console.log(i); //===> prints all numbers in the loop//
  70. }
  71.  
  72. for(var j = 10; j > 0; j--){
  73. console.log(j); //===> prints bacwards from 10 to 1//
  74. }
  75.  
  76. // Loop through arrays
  77. function loopArray(myArray){
  78. for(var a = 0; a < myArray.length; a++){
  79. console.log(myArray[a]);
  80. }
  81. }
  82. loopArray(["david", 28, "pizza"]);
  83.  
  84. function loopReverseArray(myArray){
  85. for(var b = myArray.length -1; b >= 0; b--){
  86. console.log(myArray[b]);
  87. }
  88. }
  89. loopReverseArray(["david", 28, "pizza"]);
  90.  
  91. // 2. Loop through objects using for..in
  92. function objectKeys(myObject) {
  93. for (var prop in myObject) {
  94. console.log(myObject[prop]);
  95. }
  96. }
  97. objectKeys({name: "gabby", age: 27, food: "chips"})
  98.  
  99.  
  100.  
  101. // 3. while loop
  102. var w = 7;
  103. while (w < 10) {
  104. console.log(w); //===> 7, 8, 9
  105. w++;
  106. }
  107. //the code in the loop will run as long as the variable (w) is less than 10
  108. </script></body>
  109. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement