Guest User

Untitled

a guest
Nov 16th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.80 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: Daniel Copeland
  13. // -----------------------------------------------
  14.  
  15. // -----------------------------------------------
  16. // Exercise: Fizz Buzz
  17. // -----------------------------------------------
  18.  
  19. function fizzBuzz(n) {
  20. let output="";
  21. if (n<=0) return output;
  22. for (i=1;i<=n;i++) {
  23. output+=fizzCheck(i);
  24. if (i!==n) {
  25. output+= ", ";
  26. }
  27. };
  28. return output;
  29. };
  30.  
  31. function fizzCheck(num) {
  32. let word="";
  33. if (num<=0) {
  34. n=0;
  35. }
  36. else {
  37. if (num%3==0) {
  38. word+="fizz";
  39. }
  40. if (num%5==0) {
  41. word+="buzz";
  42. }
  43. }
  44. return num + word;
  45. };
  46.  
  47. // -----------------------------------------------
  48. // Name: Daniel Copeland
  49. // -----------------------------------------------
  50.  
  51. // -----------------------------------------------
  52. // Exercise: Anagram Tester
  53. // -----------------------------------------------
  54. function areTheseAnagrams(a, b) {
  55. let aArr = a.toLowerCase().split("");
  56. let bArr = b.toLowerCase().split("");
  57. for(i=0;i<aArr.length;i++) {
  58. for (j=0;j<bArr.length;j++) {
  59. if (aArr[i]==bArr[j]) {
  60. aArr.splice(i,1);
  61.  
  62. bArr.splice(j,1);
  63. i--;
  64. }
  65. }
  66. }
  67. return aArr.length==0 && bArr.length==0;
  68. }
  69.  
  70. // -----------------------------------------------
  71. // Name: Daniel Copeland
  72. // -----------------------------------------------
  73.  
  74. // -----------------------------------------------
  75. // Exercise: Analyze Prices
  76. // -----------------------------------------------
  77. function analyzePrices(prices) {
  78. let output = {
  79. buyIndex:null,
  80. sellIndex:null
  81. };
  82. let maxDif=0;
  83. for (i=0;i<prices.length-1;i++) {
  84. for (j=i;j<prices.length;j++) {
  85. if (prices[j]-prices[i] > maxDif) {
  86. maxDif = prices[j]-prices[i];
  87. output.buyIndex=prices[i];
  88. output.sellIndex=prices[j];
  89. }
  90. }
  91. }
  92. return output;
  93.  
  94. }
  95.  
  96. // -----------------------------------------------
  97. // Name: Daniel Copeland
  98. // -----------------------------------------------
  99.  
  100. // -----------------------------------------------
  101. // Exercise: Sum Nested Arrays
  102. // -----------------------------------------------
  103. function sumNested(arr) {
  104. if (arr.length==0) return 0;
  105. let sum=0;
  106. for (i=0;i<arr.length;i++) {
  107. let test=arr[i];
  108. if (typeof test==="object") {
  109. sum+=sumNested(test);
  110. }
  111. else {
  112. sum+=test;
  113. }
  114. }
  115. return sum;
  116. }
  117.  
  118. // -----------------------------------------------
  119. // Name: Daniel Copeland
  120. // -----------------------------------------------
  121.  
  122. // -----------------------------------------------
  123. // Exercise: Word Count
  124. // -----------------------------------------------
  125.  
  126. function wordCount(sentence) {
  127. return sentence.split(" ").filter(word=>word!="").length;
  128. }
  129.  
  130. // This one was fun!
  131.  
  132. // -----------------------------------------------
  133. // Name: Daniel Copeland
  134. // -----------------------------------------------
  135.  
  136. // -----------------------------------------------
  137. // Exercise: Car
  138. // -----------------------------------------------
  139.  
  140. function Car(speed) {
  141. this._speed = speed;
  142. this.getSpeed = function() {
  143. return this._speed;
  144. };
  145. this.setSpeed = function(speed) {
  146. if (speed>=0) this._speed = speed;
  147. };
  148. this.stop = function() {
  149. this.setSpeed(0);
  150. };
  151. }
  152.  
  153. // -----------------------------------------------
  154. // Name: Daniel Copeland
  155. // -----------------------------------------------
  156.  
  157. // -----------------------------------------------
  158. // Exercise: Property Path Evaluation
  159. // -----------------------------------------------
  160. function propertyValueAt(obj, arr) {
  161. let output = 0;
  162. for (i=0;i<arr.length;i++) {
  163. output+=parseInt(rec(obj, arr[i]));
  164. }
  165. if (isNaN(output)) output = undefined;
  166. return output;
  167. }
  168.  
  169. function rec(obj,index) {
  170. return obj[index];
  171. }
  172.  
  173. // this only works for properties with a single value. I attempted to make this work for
  174. // nested properties, but ran out of time. It is shown below
  175.  
  176. /*
  177. function propertyValueAt(obj, arr) {
  178. if (arr.length == 0) return 0;
  179. let output = 0;
  180. for (i=0;i<arr.length;i++) {
  181. if (typeof arr[i]==="object") {
  182. output+=propertyValue(obj[arr[i]],arr[i]);
  183. }
  184. output+=parseInt(obj[arr[i]]);
  185. }
  186. if (isNaN(output)) output = undefined;
  187. return output;
  188. }
  189.  
  190. function rec(obj,index) {
  191. return obj[index];
  192. }
  193.  
  194.  
  195. let test = {
  196. a:1,
  197. b:{
  198. d:2,
  199. e:4
  200. }
  201. c:3
  202. };
  203. console.log(propertyValueAt(test,['a','b'));
  204. */
  205. </script>
  206.  
  207.  
  208.  
  209. <script id="jsbin-source-javascript" type="text/javascript">// -----------------------------------------------
  210. // Name: Daniel Copeland
  211. // -----------------------------------------------
  212.  
  213. // -----------------------------------------------
  214. // Exercise: Fizz Buzz
  215. // -----------------------------------------------
  216.  
  217. function fizzBuzz(n) {
  218. let output="";
  219. if (n<=0) return output;
  220. for (i=1;i<=n;i++) {
  221. output+=fizzCheck(i);
  222. if (i!==n) {
  223. output+= ", ";
  224. }
  225. };
  226. return output;
  227. };
  228.  
  229. function fizzCheck(num) {
  230. let word="";
  231. if (num<=0) {
  232. n=0;
  233. }
  234. else {
  235. if (num%3==0) {
  236. word+="fizz";
  237. }
  238. if (num%5==0) {
  239. word+="buzz";
  240. }
  241. }
  242. return num + word;
  243. };
  244.  
  245. // -----------------------------------------------
  246. // Name: Daniel Copeland
  247. // -----------------------------------------------
  248.  
  249. // -----------------------------------------------
  250. // Exercise: Anagram Tester
  251. // -----------------------------------------------
  252. function areTheseAnagrams(a, b) {
  253. let aArr = a.toLowerCase().split("");
  254. let bArr = b.toLowerCase().split("");
  255. for(i=0;i<aArr.length;i++) {
  256. for (j=0;j<bArr.length;j++) {
  257. if (aArr[i]==bArr[j]) {
  258. aArr.splice(i,1);
  259.  
  260. bArr.splice(j,1);
  261. i--;
  262. }
  263. }
  264. }
  265. return aArr.length==0 && bArr.length==0;
  266. }
  267.  
  268. // -----------------------------------------------
  269. // Name: Daniel Copeland
  270. // -----------------------------------------------
  271.  
  272. // -----------------------------------------------
  273. // Exercise: Analyze Prices
  274. // -----------------------------------------------
  275. function analyzePrices(prices) {
  276. let output = {
  277. buyIndex:null,
  278. sellIndex:null
  279. };
  280. let maxDif=0;
  281. for (i=0;i<prices.length-1;i++) {
  282. for (j=i;j<prices.length;j++) {
  283. if (prices[j]-prices[i] > maxDif) {
  284. maxDif = prices[j]-prices[i];
  285. output.buyIndex=prices[i];
  286. output.sellIndex=prices[j];
  287. }
  288. }
  289. }
  290. return output;
  291.  
  292. }
  293.  
  294. // -----------------------------------------------
  295. // Name: Daniel Copeland
  296. // -----------------------------------------------
  297.  
  298. // -----------------------------------------------
  299. // Exercise: Sum Nested Arrays
  300. // -----------------------------------------------
  301. function sumNested(arr) {
  302. if (arr.length==0) return 0;
  303. let sum=0;
  304. for (i=0;i<arr.length;i++) {
  305. let test=arr[i];
  306. if (typeof test==="object") {
  307. sum+=sumNested(test);
  308. }
  309. else {
  310. sum+=test;
  311. }
  312. }
  313. return sum;
  314. }
  315.  
  316. // -----------------------------------------------
  317. // Name: Daniel Copeland
  318. // -----------------------------------------------
  319.  
  320. // -----------------------------------------------
  321. // Exercise: Word Count
  322. // -----------------------------------------------
  323.  
  324. function wordCount(sentence) {
  325. return sentence.split(" ").filter(word=>word!="").length;
  326. }
  327.  
  328. // This one was fun!
  329.  
  330. // -----------------------------------------------
  331. // Name: Daniel Copeland
  332. // -----------------------------------------------
  333.  
  334. // -----------------------------------------------
  335. // Exercise: Car
  336. // -----------------------------------------------
  337.  
  338. function Car(speed) {
  339. this._speed = speed;
  340. this.getSpeed = function() {
  341. return this._speed;
  342. };
  343. this.setSpeed = function(speed) {
  344. if (speed>=0) this._speed = speed;
  345. };
  346. this.stop = function() {
  347. this.setSpeed(0);
  348. };
  349. }
  350.  
  351. // -----------------------------------------------
  352. // Name: Daniel Copeland
  353. // -----------------------------------------------
  354.  
  355. // -----------------------------------------------
  356. // Exercise: Property Path Evaluation
  357. // -----------------------------------------------
  358. function propertyValueAt(obj, arr) {
  359. let output = 0;
  360. for (i=0;i<arr.length;i++) {
  361. output+=parseInt(rec(obj, arr[i]));
  362. }
  363. if (isNaN(output)) output = undefined;
  364. return output;
  365. }
  366.  
  367. function rec(obj,index) {
  368. return obj[index];
  369. }
  370.  
  371. // this only works for properties with a single value. I attempted to make this work for
  372. // nested properties, but ran out of time. It is shown below
  373.  
  374. /*
  375. function propertyValueAt(obj, arr) {
  376. if (arr.length == 0) return 0;
  377. let output = 0;
  378. for (i=0;i<arr.length;i++) {
  379. if (typeof arr[i]==="object") {
  380. output+=propertyValue(obj[arr[i]],arr[i]);
  381. }
  382. output+=parseInt(obj[arr[i]]);
  383. }
  384. if (isNaN(output)) output = undefined;
  385. return output;
  386. }
  387.  
  388. function rec(obj,index) {
  389. return obj[index];
  390. }
  391.  
  392.  
  393. let test = {
  394. a:1,
  395. b:{
  396. d:2,
  397. e:4
  398. }
  399. c:3
  400. };
  401. console.log(propertyValueAt(test,['a','b'));
  402. */
  403.  
  404.  
  405.  
  406. </script></body>
  407. </html>
Add Comment
Please, Sign In to add comment