Guest User

Untitled

a guest
Jan 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.46 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. // Name: Kean Mattingly
  13. // -----------------------------------------------
  14.  
  15. // -----------------------------------------------
  16. // Exercise: Property Path Evaluation
  17. // -----------------------------------------------
  18.  
  19. function propertyValueAt(jsObject, objProperty) {
  20. var i = 0, nestedJsObject = jsObject;
  21. do {
  22. nestedJsObject = nestedJsObject[objProperty[i++]];
  23. } while (i < objProperty.length, nestedJsObject);
  24. return nestedJsObject;
  25. }
  26.  
  27. // -----------------------------------------------
  28. // Exercise: Sum Nested Arrays
  29. // -----------------------------------------------
  30.  
  31. function sumNested(numArray) {
  32. var accumulator = 0;
  33. for (var i = 0;i < numArray.length;i++) {
  34. if (numArray[i].constructor == Array) {
  35. accumulator += sumNested(numArray[i]);
  36. } else if (numArray[i] == Number) {
  37. accumulator += numArray[i];
  38. }
  39. }
  40. return accumulator;
  41. }
  42. var numArray = [1,2,3,4,[1,2,3,[1,2,3], 2], 4];
  43.  
  44. // -----------------------------------------------
  45. // Exercise: Word Count
  46. // -----------------------------------------------
  47.  
  48. function wordCount(sentence) {
  49. var possibleWords = sentence.trim().split(" ");
  50. var wordCount = 0;
  51. for (var i = 0; i < possibleWords.length; i++) {
  52. if (!!possibleWords[i].trim()) {
  53. wordCount++;
  54. }
  55. }
  56. return wordCount;
  57. }
  58.  
  59. // -----------------------------------------------
  60. // Anagram Tester
  61. // -----------------------------------------------
  62.  
  63. function areTheseAnagrams(firstString, secondString) {
  64. if (firstString.length !== secondString.length) return false;
  65. for (var i = 0; i < firstString.length; i++) {
  66. secondString = secondString.replace(firstString.charAt(i), '');
  67. }
  68. return secondString.length > 0 ? false : true;
  69. }
  70.  
  71. // -----------------------------------------------
  72. // Analyze Prices
  73. // -----------------------------------------------
  74. //this is a sin, I know
  75. function analyzePrices(priceArray) {
  76. var differenceArray = [];
  77. for (var i = 0; i < priceArray.length; i++) {
  78. var largestDifference = 0;
  79. var indexOfLargest = i;
  80. var indexOfSmallest = i;
  81. for (var j = i; j < priceArray.length; j++) {
  82. if (priceArray[j] - priceArray[i] > largestDifference) {
  83. largestDifference = priceArray[j] - priceArray[i];
  84. indexOfLargest = j;
  85. }
  86. }
  87. differenceArray.push({
  88. diff: largestDifference,
  89. largest: indexOfLargest,
  90. smallest: indexOfSmallest
  91. })
  92. }
  93. var bestBuy = differenceArray[0];
  94. for (var k = 1; k < differenceArray.length; k++) {
  95. if (bestBuy.diff < differenceArray[k].diff) {
  96. bestBuy = differenceArray[k];
  97. }
  98. }
  99. return {
  100. buyIndex: bestBuy.smallest,
  101. sellIndex: bestBuy.largest
  102. }
  103. }
  104.  
  105. // -----------------------------------------------
  106. // Exercise: Fizz Buzz
  107. // -----------------------------------------------
  108.  
  109. function fizzBuzz(num) {
  110. var fizzBuzzString = '';
  111. if (num <= 0) return fizzBuzzString;
  112. for (var i = 1; i <= num; i++) {
  113. fizzBuzzString += i;
  114. if (i % 3 == 0 && i > 0) {
  115. fizzBuzzString += 'fizz';
  116. }
  117. if (i % 5 == 0 > 0) {
  118. fizzBuzzString += 'buzz';
  119. }
  120. fizzBuzzString += i == num ? '' : ', ';
  121. }
  122. return fizzBuzzString;
  123. }
  124.  
  125. // -----------------------------------------------
  126. // Exercise: Object Oriented Programming - Car
  127. // -----------------------------------------------
  128.  
  129. function Car() {
  130. this.speed = 0;
  131. this.getSpeed = function() {
  132. return this.speed;
  133. }
  134. this.setSpeed = function(speed) {
  135. this.speed = speed;
  136. }
  137. this.stop = function() {
  138. this.speed = 0;
  139. }
  140. }
  141.  
  142. // -----------------------------------------------
  143. // Exercise:
  144. // -----------------------------------------------
  145. </script>
  146.  
  147.  
  148.  
  149. <script id="jsbin-source-javascript" type="text/javascript">// -----------------------------------------------
  150. // Name: Kean Mattingly
  151. // -----------------------------------------------
  152.  
  153. // -----------------------------------------------
  154. // Exercise: Property Path Evaluation
  155. // -----------------------------------------------
  156.  
  157. function propertyValueAt(jsObject, objProperty) {
  158. var i = 0, nestedJsObject = jsObject;
  159. do {
  160. nestedJsObject = nestedJsObject[objProperty[i++]];
  161. } while (i < objProperty.length, nestedJsObject);
  162. return nestedJsObject;
  163. }
  164.  
  165. // -----------------------------------------------
  166. // Exercise: Sum Nested Arrays
  167. // -----------------------------------------------
  168.  
  169. function sumNested(numArray) {
  170. var accumulator = 0;
  171. for (var i = 0;i < numArray.length;i++) {
  172. if (numArray[i].constructor == Array) {
  173. accumulator += sumNested(numArray[i]);
  174. } else if (numArray[i] == Number) {
  175. accumulator += numArray[i];
  176. }
  177. }
  178. return accumulator;
  179. }
  180. var numArray = [1,2,3,4,[1,2,3,[1,2,3], 2], 4];
  181.  
  182. // -----------------------------------------------
  183. // Exercise: Word Count
  184. // -----------------------------------------------
  185.  
  186. function wordCount(sentence) {
  187. var possibleWords = sentence.trim().split(" ");
  188. var wordCount = 0;
  189. for (var i = 0; i < possibleWords.length; i++) {
  190. if (!!possibleWords[i].trim()) {
  191. wordCount++;
  192. }
  193. }
  194. return wordCount;
  195. }
  196.  
  197. // -----------------------------------------------
  198. // Anagram Tester
  199. // -----------------------------------------------
  200.  
  201. function areTheseAnagrams(firstString, secondString) {
  202. if (firstString.length !== secondString.length) return false;
  203. for (var i = 0; i < firstString.length; i++) {
  204. secondString = secondString.replace(firstString.charAt(i), '');
  205. }
  206. return secondString.length > 0 ? false : true;
  207. }
  208.  
  209. // -----------------------------------------------
  210. // Analyze Prices
  211. // -----------------------------------------------
  212. //this is a sin, I know
  213. function analyzePrices(priceArray) {
  214. var differenceArray = [];
  215. for (var i = 0; i < priceArray.length; i++) {
  216. var largestDifference = 0;
  217. var indexOfLargest = i;
  218. var indexOfSmallest = i;
  219. for (var j = i; j < priceArray.length; j++) {
  220. if (priceArray[j] - priceArray[i] > largestDifference) {
  221. largestDifference = priceArray[j] - priceArray[i];
  222. indexOfLargest = j;
  223. }
  224. }
  225. differenceArray.push({
  226. diff: largestDifference,
  227. largest: indexOfLargest,
  228. smallest: indexOfSmallest
  229. })
  230. }
  231. var bestBuy = differenceArray[0];
  232. for (var k = 1; k < differenceArray.length; k++) {
  233. if (bestBuy.diff < differenceArray[k].diff) {
  234. bestBuy = differenceArray[k];
  235. }
  236. }
  237. return {
  238. buyIndex: bestBuy.smallest,
  239. sellIndex: bestBuy.largest
  240. }
  241. }
  242.  
  243. // -----------------------------------------------
  244. // Exercise: Fizz Buzz
  245. // -----------------------------------------------
  246.  
  247. function fizzBuzz(num) {
  248. var fizzBuzzString = '';
  249. if (num <= 0) return fizzBuzzString;
  250. for (var i = 1; i <= num; i++) {
  251. fizzBuzzString += i;
  252. if (i % 3 == 0 && i > 0) {
  253. fizzBuzzString += 'fizz';
  254. }
  255. if (i % 5 == 0 > 0) {
  256. fizzBuzzString += 'buzz';
  257. }
  258. fizzBuzzString += i == num ? '' : ', ';
  259. }
  260. return fizzBuzzString;
  261. }
  262.  
  263. // -----------------------------------------------
  264. // Exercise: Object Oriented Programming - Car
  265. // -----------------------------------------------
  266.  
  267. function Car() {
  268. this.speed = 0;
  269. this.getSpeed = function() {
  270. return this.speed;
  271. }
  272. this.setSpeed = function(speed) {
  273. this.speed = speed;
  274. }
  275. this.stop = function() {
  276. this.speed = 0;
  277. }
  278. }
  279.  
  280. // -----------------------------------------------
  281. // Exercise:
  282. // -----------------------------------------------
  283. </script></body>
  284. </html>
Add Comment
Please, Sign In to add comment