Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.70 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: Brian Pendleton
  13. //--------------------------------------
  14.  
  15. //--------------------------------------
  16. //Exercise: Property Path Evaluation
  17.  
  18. //take in an object and an array of a particular property of that object.
  19. function propertyValueAt(obj, arr) {
  20. //if the property passed in is not undefined, return it, otherwise return undefined
  21. if(arr !== undefined) {
  22. return arr;
  23. }
  24. else {
  25. return undefined;
  26. }
  27. }
  28. //--------------------------------------
  29.  
  30.  
  31.  
  32. //--------------------------------------
  33. //Exercise: Sum Nested Arrays
  34. //attempts to flatten the array and then add all elements...
  35. // function sumNested(arr) {
  36. // let sum = 0;
  37. // let escape = true;
  38. //if the "every" function finds something that isn't a number (like an array) flatten again
  39. // while(escape) {
  40. // let flattened = arr.reduce(flattener(), []);
  41. // // // escape = arr.every(function(val) {
  42. // // // if(typeof val !== 'number') {
  43. // // // // return false;
  44. // // // }
  45. // // // });
  46. // // escape = false;
  47. // //
  48. // // }
  49.  
  50. //using an accumulator, add things together
  51. // sum = flattened.reduce(function(accumulator, currentVal) {
  52. // return accumulator + currentVal;
  53. // });
  54. // return sum;
  55. // }
  56.  
  57.  
  58. // function flattener(accumulator, currentVal) {
  59. // return accumulator.concat(currentVal);
  60. // };
  61.  
  62.  
  63. // //should be 20
  64. // let testArr = [5, 5, 5, [1, 2, [3, 4]]];
  65. // console.log(sumNested(testArr));
  66.  
  67. //--------------------------------------
  68.  
  69.  
  70.  
  71. //--------------------------------------
  72. //Exercise: Word Count
  73. //Takes in a string input
  74. function wordCount(inputSentence) {
  75. //return the length of the array once split by spaces
  76. return inputSentence.split(" ").length;
  77. }
  78.  
  79. //--------------------------------------
  80.  
  81.  
  82.  
  83. //--------------------------------------
  84. //Exercise: Anagram Tester
  85.  
  86.  
  87. function areTheseAnagrams(string1, string2) {
  88. let allKeys = [];
  89. let numKeys = [];
  90. let allKeys2 = [];
  91. let numKeys2 = [];
  92. let isAnagram = false;
  93. string1 = string1.toLowerCase();
  94. string2 = string2.toLowerCase();
  95. //checks if they're the same length
  96. if(string1.length !== string2.length) {
  97. return false;
  98. }
  99.  
  100. for(i = 0; i < string1.length; i++) {
  101. if(!allKeys.includes(string1[i])) {
  102. allKeys.push(string1[i]);
  103. numKeys.push(1);
  104. }
  105. else if(allKeys.includes(string1[i])) {
  106. let index = allKeys.indexOf(string1[i]);
  107. numKeys[index]++;
  108. }
  109. }
  110.  
  111. for(i = 0; i < string2.length; i++) {
  112. if(!allKeys2.includes(string2[i])) {
  113. allKeys2.push(string2[i]);
  114. numKeys2.push(1);
  115. }
  116. else if(allKeys2.includes(string2[i])) {
  117. let index = allKeys2.indexOf(string2[i]);
  118. numKeys2[index]++;
  119. }
  120. }
  121. //output both strings and their letter counts
  122. console.log(allKeys + " " + numKeys);
  123. console.log(allKeys2 + " " + numKeys2);
  124. //It should theoretically work, but since the letters aren't entered in the same order, it doesn't evaluate correctly.
  125. return (allKeys == allKeys2 && numKeys == numKeys2);
  126. }
  127.  
  128.  
  129.  
  130. //--------------------------------------
  131.  
  132.  
  133.  
  134.  
  135. //--------------------------------------
  136. //Exercise: Analyze Prices
  137.  
  138. function analyzePrices(prices) {
  139. // var min = 0;
  140. // var max = 0;
  141. // var profit = 0;
  142.  
  143. var minPrice = prices.reduce(function(a, b) {
  144. return Math.min(a, b);
  145. });
  146.  
  147. var maxPrice = prices.reduce(function(a, b) {
  148. return Math.max(a, b);
  149. });
  150. //this section was going to be the real solution but I ran out of time so now you can travel through time when you buy/sell.
  151. // for(i = 0; i < prices.length; i++) {
  152. // for(j = 1; j < prices.length; j++) {
  153. // if(prices[i] < prices[j]){
  154. // if(min < prices[i]) {
  155. // min = prices[i];
  156. // }
  157. // if(max > prices[j];
  158. // if
  159. // }
  160. // }
  161. // }
  162.  
  163. var buyIndex = prices.indexOf(minPrice);
  164. var sellIndex = prices.indexOf(maxPrice);
  165. return {
  166. buyIndex: buyIndex,
  167. sellIndex: sellIndex
  168. };
  169. }
  170.  
  171.  
  172. //--------------------------------------
  173.  
  174.  
  175.  
  176.  
  177. //--------------------------------------
  178. //Exercise: Fizz Buzz
  179.  
  180. function fizzBuzz(n) {
  181. if(n <= 0) {
  182. return "";
  183. }
  184. else if(n > 0) {
  185. console.log(n);
  186. //take modulo of 3 and/or 5 where appropriate and output the associated string
  187. if((n % 3) === 0 && (n % 5) === 0) {
  188. console.log("fizzbuzz");
  189. }
  190. else if((n % 3) === 0) {
  191. console.log("fizz");
  192. }
  193. else if((n % 5) === 0) {
  194. console.log("buzz");
  195. }
  196. }
  197. }
  198.  
  199.  
  200.  
  201. //--------------------------------------
  202.  
  203. //--------------------------------------
  204. //Exercise: Object Oriented Programming - Car
  205.  
  206. const Car = {
  207. //default speed value is 0
  208. _speed: 0,
  209. getSpeed() {
  210. return this._speed;
  211. },
  212. setSpeed(speed) {
  213. this._speed = speed;
  214. },
  215. stop() {
  216. this._speed = 0;
  217. }
  218. }
  219.  
  220.  
  221. //--------------------------------------
  222. </script>
  223.  
  224.  
  225.  
  226. <script id="jsbin-source-javascript" type="text/javascript">//--------------------------------------
  227. //Name: Brian Pendleton
  228. //--------------------------------------
  229.  
  230. //--------------------------------------
  231. //Exercise: Property Path Evaluation
  232.  
  233. //take in an object and an array of a particular property of that object.
  234. function propertyValueAt(obj, arr) {
  235. //if the property passed in is not undefined, return it, otherwise return undefined
  236. if(arr !== undefined) {
  237. return arr;
  238. }
  239. else {
  240. return undefined;
  241. }
  242. }
  243. //--------------------------------------
  244.  
  245.  
  246.  
  247. //--------------------------------------
  248. //Exercise: Sum Nested Arrays
  249. //attempts to flatten the array and then add all elements...
  250. // function sumNested(arr) {
  251. // let sum = 0;
  252. // let escape = true;
  253. //if the "every" function finds something that isn't a number (like an array) flatten again
  254. // while(escape) {
  255. // let flattened = arr.reduce(flattener(), []);
  256. // // // escape = arr.every(function(val) {
  257. // // // if(typeof val !== 'number') {
  258. // // // // return false;
  259. // // // }
  260. // // // });
  261. // // escape = false;
  262. // //
  263. // // }
  264.  
  265. //using an accumulator, add things together
  266. // sum = flattened.reduce(function(accumulator, currentVal) {
  267. // return accumulator + currentVal;
  268. // });
  269. // return sum;
  270. // }
  271.  
  272.  
  273. // function flattener(accumulator, currentVal) {
  274. // return accumulator.concat(currentVal);
  275. // };
  276.  
  277.  
  278. // //should be 20
  279. // let testArr = [5, 5, 5, [1, 2, [3, 4]]];
  280. // console.log(sumNested(testArr));
  281.  
  282. //--------------------------------------
  283.  
  284.  
  285.  
  286. //--------------------------------------
  287. //Exercise: Word Count
  288. //Takes in a string input
  289. function wordCount(inputSentence) {
  290. //return the length of the array once split by spaces
  291. return inputSentence.split(" ").length;
  292. }
  293.  
  294. //--------------------------------------
  295.  
  296.  
  297.  
  298. //--------------------------------------
  299. //Exercise: Anagram Tester
  300.  
  301.  
  302. function areTheseAnagrams(string1, string2) {
  303. let allKeys = [];
  304. let numKeys = [];
  305. let allKeys2 = [];
  306. let numKeys2 = [];
  307. let isAnagram = false;
  308. string1 = string1.toLowerCase();
  309. string2 = string2.toLowerCase();
  310. //checks if they're the same length
  311. if(string1.length !== string2.length) {
  312. return false;
  313. }
  314.  
  315. for(i = 0; i < string1.length; i++) {
  316. if(!allKeys.includes(string1[i])) {
  317. allKeys.push(string1[i]);
  318. numKeys.push(1);
  319. }
  320. else if(allKeys.includes(string1[i])) {
  321. let index = allKeys.indexOf(string1[i]);
  322. numKeys[index]++;
  323. }
  324. }
  325.  
  326. for(i = 0; i < string2.length; i++) {
  327. if(!allKeys2.includes(string2[i])) {
  328. allKeys2.push(string2[i]);
  329. numKeys2.push(1);
  330. }
  331. else if(allKeys2.includes(string2[i])) {
  332. let index = allKeys2.indexOf(string2[i]);
  333. numKeys2[index]++;
  334. }
  335. }
  336. //output both strings and their letter counts
  337. console.log(allKeys + " " + numKeys);
  338. console.log(allKeys2 + " " + numKeys2);
  339. //It should theoretically work, but since the letters aren't entered in the same order, it doesn't evaluate correctly.
  340. return (allKeys == allKeys2 && numKeys == numKeys2);
  341. }
  342.  
  343.  
  344.  
  345. //--------------------------------------
  346.  
  347.  
  348.  
  349.  
  350. //--------------------------------------
  351. //Exercise: Analyze Prices
  352.  
  353. function analyzePrices(prices) {
  354. // var min = 0;
  355. // var max = 0;
  356. // var profit = 0;
  357.  
  358. var minPrice = prices.reduce(function(a, b) {
  359. return Math.min(a, b);
  360. });
  361.  
  362. var maxPrice = prices.reduce(function(a, b) {
  363. return Math.max(a, b);
  364. });
  365. //this section was going to be the real solution but I ran out of time so now you can travel through time when you buy/sell.
  366. // for(i = 0; i < prices.length; i++) {
  367. // for(j = 1; j < prices.length; j++) {
  368. // if(prices[i] < prices[j]){
  369. // if(min < prices[i]) {
  370. // min = prices[i];
  371. // }
  372. // if(max > prices[j];
  373. // if
  374. // }
  375. // }
  376. // }
  377.  
  378. var buyIndex = prices.indexOf(minPrice);
  379. var sellIndex = prices.indexOf(maxPrice);
  380. return {
  381. buyIndex: buyIndex,
  382. sellIndex: sellIndex
  383. };
  384. }
  385.  
  386.  
  387. //--------------------------------------
  388.  
  389.  
  390.  
  391.  
  392. //--------------------------------------
  393. //Exercise: Fizz Buzz
  394.  
  395. function fizzBuzz(n) {
  396. if(n <= 0) {
  397. return "";
  398. }
  399. else if(n > 0) {
  400. console.log(n);
  401. //take modulo of 3 and/or 5 where appropriate and output the associated string
  402. if((n % 3) === 0 && (n % 5) === 0) {
  403. console.log("fizzbuzz");
  404. }
  405. else if((n % 3) === 0) {
  406. console.log("fizz");
  407. }
  408. else if((n % 5) === 0) {
  409. console.log("buzz");
  410. }
  411. }
  412. }
  413.  
  414.  
  415.  
  416. //--------------------------------------
  417.  
  418. //--------------------------------------
  419. //Exercise: Object Oriented Programming - Car
  420.  
  421. const Car = {
  422. //default speed value is 0
  423. _speed: 0,
  424. getSpeed() {
  425. return this._speed;
  426. },
  427. setSpeed(speed) {
  428. this._speed = speed;
  429. },
  430. stop() {
  431. this._speed = 0;
  432. }
  433. }
  434.  
  435.  
  436. //--------------------------------------
  437.  
  438.  
  439.  
  440.  
  441.  
  442. </script></body>
  443. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement