Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. // -----------------------
  12. // Geoffrey Gibson
  13. // -----------------------
  14.  
  15. // -----------------------
  16. // Exercise: Fizz Buzz
  17. // -----------------------
  18.  
  19. function fizzBuzz(n)
  20. {
  21. // loop over the number value given
  22. for (var i = 0; i <= n; ++i)
  23. {
  24. // checks if 'i' is divisble by 3
  25. if ((i % 3) === 0)
  26. {
  27. console.log(i + 'fizz');
  28. }
  29. // checks if 'i' is divisble by 5
  30. else if ((i % 5) === 0) {
  31. console.log(i + 'buzz');
  32. }
  33. // checks if 'i' is divisble by 15
  34. else if ((i % 15) === 0) {
  35. console.log(i + 'fizzbuzz');
  36. }
  37. // else print the number as is
  38. else {
  39. console.log(i);
  40. }
  41. }
  42. }
  43. console.log(fizzBuzz(15));
  44.  
  45. // -----------------------
  46. // Exercise: Word Count
  47. // -----------------------
  48.  
  49. // come back
  50. function wordCount(sentence)
  51. {
  52. var count = 0;
  53. var str = sentence.substring(0, sentence.length;
  54. for (var i = 0; i < sentence.length; ++i)
  55. {
  56. if (!str.includes(' '))
  57. {
  58. console.log(++count);
  59. }
  60. }
  61. }
  62. console.log(wordCount('Hey There'));
  63.  
  64.  
  65. // --------------------------------
  66. // Exercise: Object-oriented - Car
  67. // --------------------------------
  68. function Car()
  69. {
  70. // if this was not set it would return null/undefined we want this defined for the output
  71. this.carSpeed = 0;
  72.  
  73. this.getSpeed = function()
  74. {
  75. // returning the value of the current speed
  76. return this.carSpeed;
  77. }
  78. this.setSpeed = function(speed)
  79. {
  80. // using the setter, return 'this' carSpeed the global variable and set it equal to value of speed
  81. this.carSpeed = speed;
  82. return this.carSpeed; // return the value after it has been reassigned to speed
  83. }
  84. this.stop = function() {
  85. // no need for a parameter to set the value to zero, just a reassignment
  86. this.carSpeed = 0;
  87. return this.carSpeed;
  88. }
  89. }
  90.  
  91. var car = new Car();
  92. console.log(car.getSpeed());
  93. car.setSpeed(10);
  94. console.log(car.getSpeed());
  95. car.stop();
  96. console.log(car.getSpeed());
  97. </script>
  98.  
  99.  
  100.  
  101. <script id="jsbin-source-javascript" type="text/javascript">// -----------------------
  102. // Geoffrey Gibson
  103. // -----------------------
  104.  
  105. // -----------------------
  106. // Exercise: Fizz Buzz
  107. // -----------------------
  108.  
  109. function fizzBuzz(n)
  110. {
  111. // loop over the number value given
  112. for (var i = 0; i <= n; ++i)
  113. {
  114. // checks if 'i' is divisble by 3
  115. if ((i % 3) === 0)
  116. {
  117. console.log(i + 'fizz');
  118. }
  119. // checks if 'i' is divisble by 5
  120. else if ((i % 5) === 0) {
  121. console.log(i + 'buzz');
  122. }
  123. // checks if 'i' is divisble by 15
  124. else if ((i % 15) === 0) {
  125. console.log(i + 'fizzbuzz');
  126. }
  127. // else print the number as is
  128. else {
  129. console.log(i);
  130. }
  131. }
  132. }
  133. console.log(fizzBuzz(15));
  134.  
  135. // -----------------------
  136. // Exercise: Word Count
  137. // -----------------------
  138.  
  139. // come back
  140. function wordCount(sentence)
  141. {
  142. var count = 0;
  143. var str = sentence.substring(0, sentence.length;
  144. for (var i = 0; i < sentence.length; ++i)
  145. {
  146. if (!str.includes(' '))
  147. {
  148. console.log(++count);
  149. }
  150. }
  151. }
  152. console.log(wordCount('Hey There'));
  153.  
  154.  
  155. // --------------------------------
  156. // Exercise: Object-oriented - Car
  157. // --------------------------------
  158. function Car()
  159. {
  160. // if this was not set it would return null/undefined we want this defined for the output
  161. this.carSpeed = 0;
  162.  
  163. this.getSpeed = function()
  164. {
  165. // returning the value of the current speed
  166. return this.carSpeed;
  167. }
  168. this.setSpeed = function(speed)
  169. {
  170. // using the setter, return 'this' carSpeed the global variable and set it equal to value of speed
  171. this.carSpeed = speed;
  172. return this.carSpeed; // return the value after it has been reassigned to speed
  173. }
  174. this.stop = function() {
  175. // no need for a parameter to set the value to zero, just a reassignment
  176. this.carSpeed = 0;
  177. return this.carSpeed;
  178. }
  179. }
  180.  
  181. var car = new Car();
  182. console.log(car.getSpeed());
  183. car.setSpeed(10);
  184. console.log(car.getSpeed());
  185. car.stop();
  186. console.log(car.getSpeed());
  187.  
  188.  
  189.  
  190.  
  191. </script></body>
  192. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement