Advertisement
Guest User

Untitled

a guest
Jan 27th, 2015
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JS Bin</title>
  6. </head>
  7. <body>
  8.  
  9. <script id="jsbin-javascript">
  10. /*WHILE Statements
  11. While (expression)
  12. statement
  13. A simple Eg:
  14. */
  15. var count = 0;
  16. while(count < 10) {
  17. console.log(count);
  18. count++;
  19. }
  20. /*DOWHILE
  21. do
  22. statement
  23. while (expression)
  24. In this the body is executed once then expression is ran
  25. Eg:
  26. */
  27. function printArray(a) {
  28. if(a.length==0)
  29. console.log("Empty Array");
  30. else {
  31. var i=0;
  32. do {
  33. console.log(a[i]);
  34. } while(++i < a.length);
  35. }
  36. }
  37. a = ["First Element","Second Element",5,10];
  38. printArray(a); //prints the array contents, otherwise will just print "Empty Array"
  39. /*FOR Statements
  40. For(initialise ; test ; increment)
  41. statement
  42. Route of passage is like this:
  43. initialise
  44. while(test) {
  45. statement;
  46. increment;
  47. }
  48. Rules
  49. You can use var in for loops as initialising
  50. Test statement is processed before each loop
  51. The increment expression then performed. Usually ++ -- or an assignment of sorts
  52. You would rewrite the 1st while program like so
  53. */
  54. var sum = 0;
  55. for(i=0 ; i<10 ; i++) {
  56. console.log(i);
  57. }
  58. //Or you could do more complex for loop
  59. for(i=0,j=10; i<j ; i++) {
  60. sum += i+j;
  61. console.log(sum); //prints with each iteration as i increases is added to previous value 10,21,33,46,60,75,91,108,126,145
  62. }
  63. /*FOR IN Statements
  64. For(variable in object)
  65. statement
  66. You can display an objects value of its property using object[variable name]
  67. For loop to display objects names and properties Eg:
  68. */
  69. {
  70. var colour,diameter=100,size=(Math.PI * Math.pow((diameter/2), 2)).toFixed(2);
  71. }
  72.  
  73. var ball = {
  74. 'colour' : "Blue",
  75. 'diameter': diameter,
  76. 'size' : size
  77. };
  78. //***VERY USEFULL***
  79.  
  80. for (var property in ball) {
  81. console.log("name: "+property+"; value: "+ball[property]);
  82. }
  83. var myObject = {x:10, y:23, z:32};
  84. var myArray = [];
  85. var k = 0;
  86. for(myArray[k++] in myObject) { //Adds the objects properties to the array
  87. }
  88. console.log(myArray);
  89.  
  90. </script>
  91.  
  92.  
  93.  
  94. <script id="jsbin-source-javascript" type="text/javascript">/*WHILE Statements
  95. While (expression)
  96. statement
  97. A simple Eg:
  98. */
  99. var count = 0;
  100. while(count < 10) {
  101. console.log(count);
  102. count++;
  103. }
  104. /*DOWHILE
  105. do
  106. statement
  107. while (expression)
  108. In this the body is executed once then expression is ran
  109. Eg:
  110. */
  111. function printArray(a) {
  112. if(a.length==0)
  113. console.log("Empty Array");
  114. else {
  115. var i=0;
  116. do {
  117. console.log(a[i]);
  118. } while(++i < a.length);
  119. }
  120. }
  121. a = ["First Element","Second Element",5,10];
  122. printArray(a); //prints the array contents, otherwise will just print "Empty Array"
  123. /*FOR Statements
  124. For(initialise ; test ; increment)
  125. statement
  126. Route of passage is like this:
  127. initialise
  128. while(test) {
  129. statement;
  130. increment;
  131. }
  132. Rules
  133. You can use var in for loops as initialising
  134. Test statement is processed before each loop
  135. The increment expression then performed. Usually ++ -- or an assignment of sorts
  136. You would rewrite the 1st while program like so
  137. */
  138. var sum = 0;
  139. for(i=0 ; i<10 ; i++) {
  140. console.log(i);
  141. }
  142. //Or you could do more complex for loop
  143. for(i=0,j=10; i<j ; i++) {
  144. sum += i+j;
  145. console.log(sum); //prints with each iteration as i increases is added to previous value 10,21,33,46,60,75,91,108,126,145
  146. }
  147. /*FOR IN Statements
  148. For(variable in object)
  149. statement
  150. You can display an objects value of its property using object[variable name]
  151. For loop to display objects names and properties Eg:
  152. */
  153. {
  154. var colour,diameter=100,size=(Math.PI * Math.pow((diameter/2), 2)).toFixed(2);
  155. }
  156.  
  157. var ball = {
  158. 'colour' : "Blue",
  159. 'diameter': diameter,
  160. 'size' : size
  161. };
  162. //***VERY USEFULL***
  163.  
  164. for (var property in ball) {
  165. console.log("name: "+property+"; value: "+ball[property]);
  166. }
  167. var myObject = {x:10, y:23, z:32};
  168. var myArray = [];
  169. var k = 0;
  170. for(myArray[k++] in myObject) { //Adds the objects properties to the array
  171. }
  172. console.log(myArray);
  173. </script></body>
  174. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement