Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.67 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="description" content="[Control Flow]">
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width">
  7. <title>Control Flow</title>
  8. </head>
  9. <body>
  10.  
  11. <script id="jsbin-javascript">
  12. /* Control Flow is about *controlling* the *flow* of activity within our
  13. * code through conditional statements. There are several types of conditional
  14. * statements; let's go through them together! */
  15.  
  16. // Let's start with the if statement; it's pretty simple.
  17. // An if statement executes a block of code **if** a certain condition is true.
  18. // Example:
  19.  
  20. var ageDavid = 33;
  21. var ageClassAvg = 25;
  22.  
  23. if (ageDavid > ageClassAvg) {
  24. itsTrue = "David is an old fogey";
  25. }
  26. console.log(itsTrue); // prints "David is an old fogey" --- hey, watch it
  27.  
  28. // But if the if statement isn't true, it throws an error.
  29.  
  30. var heightDavid = 72;
  31. var heightClassAvg = 68;
  32.  
  33. if (heightDavid < heightClassAvg) {
  34. heShort = "David is also teeny-tiny";
  35. }
  36. console.log(heShort); // throws an error
  37.  
  38.  
  39. // This is where an if-else statement comes in handy...it gives you multiple
  40. // options for controlling that flow!
  41.  
  42. if (heightDavid < heightClassAvg) {
  43. heShort = "David is also teeny-tiny";
  44. }
  45. else if (heightDavid > heightClassAvg) {
  46. heShort = "At least David is tall"
  47. }
  48. console.log(heShort); // prints "At least David is tall"
  49.  
  50. // if-else statements can provide several options for which code to execute.
  51.  
  52. var timeFly; // minutes until flight leaves
  53. timeFly = 20;
  54.  
  55. if (timeFly > 180) {
  56. dadMood = "Not worried yet";
  57. }
  58. else if (timeFly > 60) {
  59. dadMood = "We need to get to the airport...";
  60. }
  61. else if (timeFly > 30) {
  62. dadMood = "We're gonna miss our flight!";
  63. }
  64. else if (timeFly > 10) {
  65. dadMood = "Two thousand dollars worth of tickets, down the drain";
  66. }
  67. else if (timeFly >= 0) {
  68. dadMood = "Next time, I'll just go by myself";
  69. }
  70.  
  71. console.log(dadMood);
  72. // prints "Two thousand dolars worth of tickets, down the drain"
  73.  
  74. /* The very last "else if" in the example above would be better written as an
  75. * else statement. An else statement essentially examines all the if and else
  76. * if conditions and tells the computer, "If none of the above is true, just do this."
  77. *
  78. * The last else if could be written:
  79. * else {
  80. * dadMood = "Next time, I'll just go by myself
  81. * }
  82. *
  83. * Here's a full-blown example: */
  84.  
  85. var numCats; // Number of cats someone owns
  86. numCats = 3
  87.  
  88. if (numCats === 0) {
  89. friendsThink = "General concern";
  90. }
  91. else if (numCats === 1) {
  92. friendsThink = "A pet is good when you're going through a break up";
  93. }
  94. else if (numCats === 2) {
  95. friendsThink = "Well they can each have a friend!";
  96. }
  97. else if (numCats === 3) {
  98. friendsThink = "Should we be worried?";
  99. }
  100. else {
  101. friendsThink = "We should recommend a therapist.";
  102. }
  103.  
  104. console.log(friendsThink); // prints "Should we be worried?"
  105.  
  106. // A switch statement is a more elegant way of writing out an if-else if-else
  107. // statement. It relies on cases, breaks, and a default rather than conditionals.
  108. // I'm sticking with if-else structure for now because you never forget your
  109. // first love. But here's a switch example for when hit mid-life crisis stage:
  110.  
  111. var dessert = "cookies";
  112.  
  113. switch(dessert) {
  114. case "cake":
  115. console.log("Is it someone's birthday?");
  116. break;
  117. case "cookies":
  118. console.log("Get some milk");
  119. break;
  120. case "ice cream":
  121. console.log("Don't let it melt");
  122. default:
  123. console.log("No dessert, I'm on a diet")
  124. }
  125.  
  126. //prints "Get some milk"
  127. </script>
  128.  
  129.  
  130.  
  131. <script id="jsbin-source-javascript" type="text/javascript">/* Control Flow is about *controlling* the *flow* of activity within our
  132. * code through conditional statements. There are several types of conditional
  133. * statements; let's go through them together! */
  134.  
  135. // Let's start with the if statement; it's pretty simple.
  136. // An if statement executes a block of code **if** a certain condition is true.
  137. // Example:
  138.  
  139. var ageDavid = 33;
  140. var ageClassAvg = 25;
  141.  
  142. if (ageDavid > ageClassAvg) {
  143. itsTrue = "David is an old fogey";
  144. }
  145. console.log(itsTrue); // prints "David is an old fogey" --- hey, watch it
  146.  
  147. // But if the if statement isn't true, it throws an error.
  148.  
  149. var heightDavid = 72;
  150. var heightClassAvg = 68;
  151.  
  152. if (heightDavid < heightClassAvg) {
  153. heShort = "David is also teeny-tiny";
  154. }
  155. console.log(heShort); // throws an error
  156.  
  157.  
  158. // This is where an if-else statement comes in handy...it gives you multiple
  159. // options for controlling that flow!
  160.  
  161. if (heightDavid < heightClassAvg) {
  162. heShort = "David is also teeny-tiny";
  163. }
  164. else if (heightDavid > heightClassAvg) {
  165. heShort = "At least David is tall"
  166. }
  167. console.log(heShort); // prints "At least David is tall"
  168.  
  169. // if-else statements can provide several options for which code to execute.
  170.  
  171. var timeFly; // minutes until flight leaves
  172. timeFly = 20;
  173.  
  174. if (timeFly > 180) {
  175. dadMood = "Not worried yet";
  176. }
  177. else if (timeFly > 60) {
  178. dadMood = "We need to get to the airport...";
  179. }
  180. else if (timeFly > 30) {
  181. dadMood = "We're gonna miss our flight!";
  182. }
  183. else if (timeFly > 10) {
  184. dadMood = "Two thousand dollars worth of tickets, down the drain";
  185. }
  186. else if (timeFly >= 0) {
  187. dadMood = "Next time, I'll just go by myself";
  188. }
  189.  
  190. console.log(dadMood);
  191. // prints "Two thousand dolars worth of tickets, down the drain"
  192.  
  193. /* The very last "else if" in the example above would be better written as an
  194. * else statement. An else statement essentially examines all the if and else
  195. * if conditions and tells the computer, "If none of the above is true, just do this."
  196. *
  197. * The last else if could be written:
  198. * else {
  199. * dadMood = "Next time, I'll just go by myself
  200. * }
  201. *
  202. * Here's a full-blown example: */
  203.  
  204. var numCats; // Number of cats someone owns
  205. numCats = 3
  206.  
  207. if (numCats === 0) {
  208. friendsThink = "General concern";
  209. }
  210. else if (numCats === 1) {
  211. friendsThink = "A pet is good when you're going through a break up";
  212. }
  213. else if (numCats === 2) {
  214. friendsThink = "Well they can each have a friend!";
  215. }
  216. else if (numCats === 3) {
  217. friendsThink = "Should we be worried?";
  218. }
  219. else {
  220. friendsThink = "We should recommend a therapist.";
  221. }
  222.  
  223. console.log(friendsThink); // prints "Should we be worried?"
  224.  
  225. // A switch statement is a more elegant way of writing out an if-else if-else
  226. // statement. It relies on cases, breaks, and a default rather than conditionals.
  227. // I'm sticking with if-else structure for now because you never forget your
  228. // first love. But here's a switch example for when hit mid-life crisis stage:
  229.  
  230. var dessert = "cookies";
  231.  
  232. switch(dessert) {
  233. case "cake":
  234. console.log("Is it someone's birthday?");
  235. break;
  236. case "cookies":
  237. console.log("Get some milk");
  238. break;
  239. case "ice cream":
  240. console.log("Don't let it melt");
  241. default:
  242. console.log("No dessert, I'm on a diet")
  243. }
  244.  
  245. //prints "Get some milk"</script></body>
  246. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement