Guest User

Untitled

a guest
Jan 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.31 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: Matt Huberty
  13. //---------------------------------
  14.  
  15. //---------------------------------
  16. // Exercise: Property Path Evaluation
  17. //---------------------------------
  18.  
  19. function propertyValueAt(obj, arr){
  20. let path = arr.join(".");
  21. ".".concat(path);
  22. let ans = "obj".concat(".").concat(path)
  23. //I have read warnings about using eval(), would prefer to refactor this to exlude it
  24. return eval(ans);
  25. }
  26.  
  27. let myObj = {
  28. a: 1,
  29. b: {
  30. c: 2,
  31. d: 3
  32. }
  33. }
  34.  
  35. console.log(propertyValueAt(myObj, ["b","c"]))
  36. console.log(propertyValueAt(myObj, ["b"]))
  37. console.log(propertyValueAt(myObj, ["z"]))
  38.  
  39.  
  40. //---------------------------------
  41. // Name: Matt Huberty
  42. //---------------------------------
  43.  
  44. //---------------------------------
  45. // Exercise: Sum Nested Arrays
  46. //---------------------------------
  47.  
  48. function sumNested(arr){
  49.  
  50. //Input validation
  51. if(!Array.isArray(arr) || arr.length === 0){
  52. return 0;
  53. }
  54.  
  55. //Flatten any input array
  56. function flattenArr(nested){
  57.  
  58. const notNested = nested.reduce((prev,next)=>{
  59. //console.log("reducing")
  60. //console.log(Array.isArray(prev));
  61. if(Array.isArray(prev)){
  62. return prev.concat((Array.isArray(next) ? flattenArr(next) : next));
  63. } else{
  64. const startArr = [];
  65. startArr.push(prev);
  66. return startArr.concat((Array.isArray(next) ? flattenArr(next) : next));
  67. }
  68. })
  69.  
  70. return notNested;
  71. }
  72. const flatArr = flattenArr(arr);
  73. const sum = flatArr.reduce((prev, next)=>{
  74. return prev + next;
  75. })
  76.  
  77. return sum;
  78. }
  79.  
  80. console.log(sumNested([1,1,1,[3,4,[8]],[5]]));
  81. console.log(sumNested([]));
  82. console.log(sumNested("im not an array"));
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89. //---------------------------------
  90. // Name: Matt Huberty
  91. //---------------------------------
  92.  
  93. //---------------------------------
  94. // Exercise: Word Count
  95. //---------------------------------
  96.  
  97. function wordCount(sentence){
  98. let filtered = sentence.match(/[ a-z0-9.,\/#!$%\^&\*;:{}=\-_`~()]/gi).join("");
  99.  
  100. console.log(filtered);
  101. return filtered.split(" ").length;
  102. }
  103.  
  104. console.log(wordCount("Hey the*%*%* I am @##$ 9239393 ...,"))
  105. console.log(wordCount("I want TO BREAK, This. Thing."))
  106. console.log(wordCount("$$$ $$$ %%% ^^^"))
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115. // ---------------------------------
  116. // Name: Matt Huberty
  117. // ---------------------------------
  118.  
  119. // ---------------------------------
  120. // Exercise: Anagram Tester
  121. // ---------------------------------
  122.  
  123. function areTheseAnagrams(str1, str2){
  124.  
  125. //Input validation
  126. if(typeof(str1) !== "string" || typeof(str2) !== "string" || arguments.length !== 2){
  127. return "Please input two strings to test";
  128. }
  129.  
  130. //Ignore cases
  131. const arr1 = str1.toLowerCase().split("");
  132. const arr2 = str2.toLowerCase().split("");
  133.  
  134. //Test all letters
  135. return arr1.every(function(el){
  136. return arr2.indexOf(el) >= 0 ? true : false;
  137. })
  138. }
  139.  
  140. console.log(areTheseAnagrams("abc", "bca"));
  141. console.log(areTheseAnagrams("def", "hgh"));
  142. console.log(areTheseAnagrams("def", "DEF"));
  143. console.log(areTheseAnagrams("def"));
  144. console.log(areTheseAnagrams("def", 2));
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152. //---------------------------------
  153. // Name: Matt Huberty
  154. //---------------------------------
  155.  
  156. //---------------------------------
  157. // Exercise: Analyze Prices
  158. //---------------------------------
  159.  
  160. function analyzePrices(arr){
  161.  
  162. //We need to buy low and sell high
  163. let largestProfit = 0;
  164. let buyInd = null;
  165. let sellInd = null;
  166.  
  167. //Logic for finding the greatest increase from left to right
  168. arr.forEach((el, ind)=>{
  169. for(let i=ind+1; i<arr.length; i++){
  170. if(arr[i] - el > largestProfit){
  171. largestProfit = arr[i] - el;
  172. buyInd = ind;
  173. sellInd = i;
  174. }
  175. }
  176. })
  177.  
  178. //Create an object with restults from the calculations above
  179. let analysis = {
  180. buyIndex: buyInd,
  181. sellIndex: sellInd
  182. }
  183.  
  184. return analysis;
  185. //Because this function loops through the array, and adds an additional loop for each element,
  186. //this would not be efficient with sufficiently large data sets. It has a runtime complexity
  187. //of O(n^2)
  188. }
  189.  
  190. console.log(analyzePrices([1,2,3,4,5,6,7,8,9]));
  191. console.log(analyzePrices([9,8,7,6,5,4]));
  192. console.log(analyzePrices([1,8,2,20,1,70,0,100]));
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201. //---------------------------------
  202. // Name: Matt Huberty
  203. //---------------------------------
  204.  
  205. //---------------------------------
  206. // Exercise: Fizz Buzz
  207. //---------------------------------
  208.  
  209. function fizzBuzz(n){
  210.  
  211. //Input Validation
  212. if(n <= 0 || typeof(n) !== "number"){
  213. return "";
  214. }
  215.  
  216. let outputString = "";
  217.  
  218. for(let i=1; i<=n; i++){
  219. outputString += i;
  220. if(i % 3 === 0 && i % 5 === 0){
  221. outputString += "fizzbuzz";
  222. } else if(i % 3 === 0){
  223. outputString += "fizz ";
  224. } else if(i % 5 === 0){
  225. outputString += "buzz";
  226. }
  227. if(i<n) outputString += ",";
  228. }
  229.  
  230. return outputString;
  231. }
  232.  
  233. console.log(fizzBuzz(0));
  234. console.log(fizzBuzz(15));
  235. console.log(fizzBuzz(30));
  236. console.log(fizzBuzz(-1));
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243. //---------------------------------
  244. // Name: Matt Huberty
  245. //---------------------------------
  246.  
  247. //---------------------------------
  248. // Exercise: Object Oriented Programming - Car
  249. //---------------------------------
  250.  
  251. function Car(speed){
  252. this.speed = 0;
  253. this.getSpeed = ()=>{
  254. return this.speed;
  255. }
  256. this.setSpeed = (newSpeed)=>{
  257. this.speed = newSpeed;
  258. }
  259. this.stop = ()=>{
  260. this.speed = 0;
  261. }
  262. }
  263.  
  264. let car = new Car();
  265. console.log(car.getSpeed());
  266. car.setSpeed(10);
  267. console.log(car.getSpeed());
  268. car.stop();
  269. console.log(car.getSpeed());
  270. car.setSpeed(100);
  271. console.log(car.getSpeed());
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281. //Bonus
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288. //---------------------------------
  289. // Name: Matt Huberty
  290. //---------------------------------
  291.  
  292. //---------------------------------
  293. // Exercise: Calculate Bowling Score
  294. //---------------------------------
  295.  
  296.  
  297. //Running out of time!...giving this a shot anyway
  298.  
  299. function calculateBowlingScore(str){
  300.  
  301. //Function to convert X, /, and - to point values
  302. function toPointValue(symb){
  303. if(symb === "X"){
  304. return 10;
  305. } else if(symb === "/"){
  306. return 10;
  307. } else if(symb === "-"){
  308. return 0;
  309. } else{
  310. return symb;
  311. }
  312. return "whoops";
  313. }
  314.  
  315. const scoreArr = str.split("");
  316.  
  317. //Make new array that converts each item in old array to a score
  318. const pointValues = scoreArr.map((el, ind)=>{
  319. if(el === "X"){
  320. return 10 + toPointValue(scoreArr[ind+1] + toPointValue(scoreArr[ind+2]));
  321. } else if(el === "/"){
  322. return 10 + toPointValue(scoreArr[ind+1]);
  323. } else if(el === "-"){
  324. return 0;
  325. } else{
  326. //log something to console
  327. }
  328. })
  329.  
  330. return pointValues;
  331. }
  332.  
  333. console.log(calculateBowlingScore("XX2345/234-2"))
  334. </script>
  335.  
  336.  
  337.  
  338. <script id="jsbin-source-javascript" type="text/javascript">//---------------------------------
  339. // Name: Matt Huberty
  340. //---------------------------------
  341.  
  342. //---------------------------------
  343. // Exercise: Property Path Evaluation
  344. //---------------------------------
  345.  
  346. function propertyValueAt(obj, arr){
  347. let path = arr.join(".");
  348. ".".concat(path);
  349. let ans = "obj".concat(".").concat(path)
  350. //I have read warnings about using eval(), would prefer to refactor this to exlude it
  351. return eval(ans);
  352. }
  353.  
  354. let myObj = {
  355. a: 1,
  356. b: {
  357. c: 2,
  358. d: 3
  359. }
  360. }
  361.  
  362. console.log(propertyValueAt(myObj, ["b","c"]))
  363. console.log(propertyValueAt(myObj, ["b"]))
  364. console.log(propertyValueAt(myObj, ["z"]))
  365.  
  366.  
  367. //---------------------------------
  368. // Name: Matt Huberty
  369. //---------------------------------
  370.  
  371. //---------------------------------
  372. // Exercise: Sum Nested Arrays
  373. //---------------------------------
  374.  
  375. function sumNested(arr){
  376.  
  377. //Input validation
  378. if(!Array.isArray(arr) || arr.length === 0){
  379. return 0;
  380. }
  381.  
  382. //Flatten any input array
  383. function flattenArr(nested){
  384.  
  385. const notNested = nested.reduce((prev,next)=>{
  386. //console.log("reducing")
  387. //console.log(Array.isArray(prev));
  388. if(Array.isArray(prev)){
  389. return prev.concat((Array.isArray(next) ? flattenArr(next) : next));
  390. } else{
  391. const startArr = [];
  392. startArr.push(prev);
  393. return startArr.concat((Array.isArray(next) ? flattenArr(next) : next));
  394. }
  395. })
  396.  
  397. return notNested;
  398. }
  399. const flatArr = flattenArr(arr);
  400. const sum = flatArr.reduce((prev, next)=>{
  401. return prev + next;
  402. })
  403.  
  404. return sum;
  405. }
  406.  
  407. console.log(sumNested([1,1,1,[3,4,[8]],[5]]));
  408. console.log(sumNested([]));
  409. console.log(sumNested("im not an array"));
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416. //---------------------------------
  417. // Name: Matt Huberty
  418. //---------------------------------
  419.  
  420. //---------------------------------
  421. // Exercise: Word Count
  422. //---------------------------------
  423.  
  424. function wordCount(sentence){
  425. let filtered = sentence.match(/[ a-z0-9.,\/#!$%\^&\*;:{}=\-_`~()]/gi).join("");
  426.  
  427. console.log(filtered);
  428. return filtered.split(" ").length;
  429. }
  430.  
  431. console.log(wordCount("Hey the*%*%* I am @##$ 9239393 ...,"))
  432. console.log(wordCount("I want TO BREAK, This. Thing."))
  433. console.log(wordCount("$$$ $$$ %%% ^^^"))
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442. // ---------------------------------
  443. // Name: Matt Huberty
  444. // ---------------------------------
  445.  
  446. // ---------------------------------
  447. // Exercise: Anagram Tester
  448. // ---------------------------------
  449.  
  450. function areTheseAnagrams(str1, str2){
  451.  
  452. //Input validation
  453. if(typeof(str1) !== "string" || typeof(str2) !== "string" || arguments.length !== 2){
  454. return "Please input two strings to test";
  455. }
  456.  
  457. //Ignore cases
  458. const arr1 = str1.toLowerCase().split("");
  459. const arr2 = str2.toLowerCase().split("");
  460.  
  461. //Test all letters
  462. return arr1.every(function(el){
  463. return arr2.indexOf(el) >= 0 ? true : false;
  464. })
  465. }
  466.  
  467. console.log(areTheseAnagrams("abc", "bca"));
  468. console.log(areTheseAnagrams("def", "hgh"));
  469. console.log(areTheseAnagrams("def", "DEF"));
  470. console.log(areTheseAnagrams("def"));
  471. console.log(areTheseAnagrams("def", 2));
  472.  
  473.  
  474.  
  475.  
  476.  
  477.  
  478.  
  479. //---------------------------------
  480. // Name: Matt Huberty
  481. //---------------------------------
  482.  
  483. //---------------------------------
  484. // Exercise: Analyze Prices
  485. //---------------------------------
  486.  
  487. function analyzePrices(arr){
  488.  
  489. //We need to buy low and sell high
  490. let largestProfit = 0;
  491. let buyInd = null;
  492. let sellInd = null;
  493.  
  494. //Logic for finding the greatest increase from left to right
  495. arr.forEach((el, ind)=>{
  496. for(let i=ind+1; i<arr.length; i++){
  497. if(arr[i] - el > largestProfit){
  498. largestProfit = arr[i] - el;
  499. buyInd = ind;
  500. sellInd = i;
  501. }
  502. }
  503. })
  504.  
  505. //Create an object with restults from the calculations above
  506. let analysis = {
  507. buyIndex: buyInd,
  508. sellIndex: sellInd
  509. }
  510.  
  511. return analysis;
  512. //Because this function loops through the array, and adds an additional loop for each element,
  513. //this would not be efficient with sufficiently large data sets. It has a runtime complexity
  514. //of O(n^2)
  515. }
  516.  
  517. console.log(analyzePrices([1,2,3,4,5,6,7,8,9]));
  518. console.log(analyzePrices([9,8,7,6,5,4]));
  519. console.log(analyzePrices([1,8,2,20,1,70,0,100]));
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528. //---------------------------------
  529. // Name: Matt Huberty
  530. //---------------------------------
  531.  
  532. //---------------------------------
  533. // Exercise: Fizz Buzz
  534. //---------------------------------
  535.  
  536. function fizzBuzz(n){
  537.  
  538. //Input Validation
  539. if(n <= 0 || typeof(n) !== "number"){
  540. return "";
  541. }
  542.  
  543. let outputString = "";
  544.  
  545. for(let i=1; i<=n; i++){
  546. outputString += i;
  547. if(i % 3 === 0 && i % 5 === 0){
  548. outputString += "fizzbuzz";
  549. } else if(i % 3 === 0){
  550. outputString += "fizz ";
  551. } else if(i % 5 === 0){
  552. outputString += "buzz";
  553. }
  554. if(i<n) outputString += ",";
  555. }
  556.  
  557. return outputString;
  558. }
  559.  
  560. console.log(fizzBuzz(0));
  561. console.log(fizzBuzz(15));
  562. console.log(fizzBuzz(30));
  563. console.log(fizzBuzz(-1));
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570. //---------------------------------
  571. // Name: Matt Huberty
  572. //---------------------------------
  573.  
  574. //---------------------------------
  575. // Exercise: Object Oriented Programming - Car
  576. //---------------------------------
  577.  
  578. function Car(speed){
  579. this.speed = 0;
  580. this.getSpeed = ()=>{
  581. return this.speed;
  582. }
  583. this.setSpeed = (newSpeed)=>{
  584. this.speed = newSpeed;
  585. }
  586. this.stop = ()=>{
  587. this.speed = 0;
  588. }
  589. }
  590.  
  591. let car = new Car();
  592. console.log(car.getSpeed());
  593. car.setSpeed(10);
  594. console.log(car.getSpeed());
  595. car.stop();
  596. console.log(car.getSpeed());
  597. car.setSpeed(100);
  598. console.log(car.getSpeed());
  599.  
  600.  
  601.  
  602.  
  603.  
  604.  
  605.  
  606.  
  607.  
  608. //Bonus
  609.  
  610.  
  611.  
  612.  
  613.  
  614.  
  615. //---------------------------------
  616. // Name: Matt Huberty
  617. //---------------------------------
  618.  
  619. //---------------------------------
  620. // Exercise: Calculate Bowling Score
  621. //---------------------------------
  622.  
  623.  
  624. //Running out of time!...giving this a shot anyway
  625.  
  626. function calculateBowlingScore(str){
  627.  
  628. //Function to convert X, /, and - to point values
  629. function toPointValue(symb){
  630. if(symb === "X"){
  631. return 10;
  632. } else if(symb === "/"){
  633. return 10;
  634. } else if(symb === "-"){
  635. return 0;
  636. } else{
  637. return symb;
  638. }
  639. return "whoops";
  640. }
  641.  
  642. const scoreArr = str.split("");
  643.  
  644. //Make new array that converts each item in old array to a score
  645. const pointValues = scoreArr.map((el, ind)=>{
  646. if(el === "X"){
  647. return 10 + toPointValue(scoreArr[ind+1] + toPointValue(scoreArr[ind+2]));
  648. } else if(el === "/"){
  649. return 10 + toPointValue(scoreArr[ind+1]);
  650. } else if(el === "-"){
  651. return 0;
  652. } else{
  653. //log something to console
  654. }
  655. })
  656.  
  657. return pointValues;
  658. }
  659.  
  660. console.log(calculateBowlingScore("XX2345/234-2"))
  661.  
  662.  
  663.  
  664.  
  665. </script></body>
  666. </html>
Add Comment
Please, Sign In to add comment