Guest User

Untitled

a guest
Jan 21st, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.11 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: Brad Francis
  13. //---------------------------------------
  14.  
  15. //---------------------------------------
  16. // Exercise: Property Path Evaluation
  17. //---------------------------------------
  18.  
  19. function propertyValueAt(object, attributes) {
  20. var vals = [];
  21. attributes.forEach(function(attr){
  22. if(Array.isArray(attr))
  23. propertValueAtRec(vals, attr, attributes);
  24. else
  25. vals.push(object[attr]);
  26. });
  27. return vals;
  28. }
  29.  
  30. function propertValueAtRec(vals, object, attributes) {
  31. attributes.forEach(function(attr){
  32. if(typeof attr === "object")
  33. propertValueAtRec(vals, attr, attributes);
  34. else
  35. vals.push(object[attr]);
  36. });
  37. return vals;
  38. }
  39.  
  40. //---------------------------------------
  41. // Exercise: Sum Nested Arrays
  42. //---------------------------------------
  43.  
  44. var sum;
  45.  
  46. function sumNested(arr) {
  47. sum = 0;
  48. sumNestedRec(arr);
  49. return sum;
  50. }
  51.  
  52. function sumNestedRec(arr) {
  53. arr.forEach(function(el){
  54. if(Array.isArray(el))
  55. sumNestedRec(el);
  56. else
  57. sum += el;
  58. });
  59. }
  60.  
  61. //---------------------------------------
  62. // Exercise: Word Count
  63. //---------------------------------------
  64.  
  65. function wordCount(sentence) {
  66.  
  67. var midWord = false;
  68. var count = 0;
  69. var letters = sentence.split("");
  70.  
  71. letters.forEach(function(char){
  72. if(char === ' ') // if space
  73. midWord = false;
  74. else if (!midWord){ // if new word
  75. count++;
  76. midWord = true;
  77. }
  78. });
  79.  
  80. return count;
  81. }
  82.  
  83. //---------------------------------------
  84. // Exercise: Anagram Tester
  85. //---------------------------------------
  86.  
  87. function areTheseAnagrams(word1, word2) {
  88. var isAnagram = false;
  89. var isInBoth = false;
  90. var word1letters = word1.split("");
  91. var word2letters = word2.split("");
  92. for(var i in word1letters) {
  93. isInBoth = false;
  94. for(var j in word2letters) {
  95. if(word1letters[i] === word2letters[j])
  96. isInBoth = true;
  97. }
  98. if(isInBoth)
  99. isAnagram = true;
  100. else
  101. return false;
  102. }
  103.  
  104. return isAnagram;
  105. }
  106.  
  107. //---------------------------------------
  108. // Exercise: Analyze Prices
  109. //---------------------------------------
  110.  
  111. function analyzePrices(prices) {
  112. var analysis = new Object();
  113. /*
  114. var buy, sell;
  115. while ((buy = calcBuyIndex(prices)) === null) {}
  116. while ((sell = calcSellIndex(prices)) === null) {}
  117. if(buy === null || sell === null) {
  118. analysis.buyIndex = null;
  119. analysis.sellIndex = null;
  120. }
  121. else {
  122. analysis.buyIndex = buy;
  123. analysis.sellIndex = sell;
  124. }
  125. */
  126.  
  127. var maxProfit = 0;
  128. var buyInd;
  129. var sellInd;
  130. var currProfit;
  131. for(var i = 0; i < prices.length; i++){
  132. for(var j = 0; j < prices.length; j++){
  133. currProfit = prices[j] - prices[i];
  134. if(currProfit > maxProfit && i < j){
  135. maxProfit = currProfit;
  136. buyInd = i;
  137. sellInd = j;
  138. }
  139. }
  140. }
  141. if (maxProfit === 0){
  142. buyInd = null;
  143. sellInd = null;
  144. }
  145. analysis.buyIndex = buyInd;
  146. analysis.sellIndex = sellInd;
  147. return analysis;
  148. }
  149.  
  150. /*
  151. function indexOfMin(arr){
  152. var minIndex = 0;
  153. for (var i in arr){
  154. if (arr[i] < arr[minIndex])
  155. minIndex = i;
  156. }
  157. }
  158.  
  159. function indexOfMax(arr){
  160. var maxIndex = 0;
  161. for (var i in arr){
  162. if (arr[i] > arr[maxIndex])
  163. maxIndex = i;
  164. }
  165. }
  166.  
  167. function calcBuyIndex(arr){
  168. var minIndex = indexOfMin(arr);
  169. var maxIndex = indexOfMax(arr);
  170. if (maxIndex <= minIndex){
  171. arr.splice(minIndex, 1);
  172. return null;
  173. }
  174. else
  175. return minIndex;
  176. }
  177.  
  178. function calcSellIndex(arr){
  179. var minIndex = indexOfMin(arr);
  180. var maxIndex = indexOfMax(arr);
  181. if (maxIndex <= minIndex){
  182. arr.splice(maxIndex, 1);
  183. return null;
  184. }
  185. else
  186. return maxIndex;
  187. }
  188. */
  189.  
  190. //---------------------------------------
  191. // Exercise: Fizz Buzz
  192. //---------------------------------------
  193.  
  194. function fizzBuzz(n) {
  195. var output = "";
  196. if (n > 0) {
  197. for (var i = 1; i <= n; i++) {
  198. output += i;
  199. if(i % 3 === 0)
  200. output += "fizz";
  201. if(i % 5 === 0)
  202. output += "buzz";
  203. if (i < n)
  204. output += ", ";
  205. }
  206. }
  207. return output;
  208. }
  209.  
  210. //---------------------------------------
  211. // Exercise: OOP - Car
  212. //---------------------------------------
  213.  
  214. class Car {
  215.  
  216. constructor(){
  217. this.speed = 0;
  218. }
  219.  
  220. getSpeed() { return this.speed; }
  221. setSpeed(speed) { this.speed = speed; }
  222. stop() { this.speed = 0; }
  223. }
  224.  
  225. //---------------------------------------
  226. // Bonus: OCalculate Bowling Score
  227. //---------------------------------------
  228.  
  229. function calculateBowlingScores(scores){
  230. var score = 0;
  231. var scoreLetters = scores.split("");
  232. scoreLetters.forEach(function(el){
  233. switch (el) {
  234. case 'X':
  235. score += 20;
  236. break;
  237. case '/':
  238. score += 10;
  239. break;
  240. case '-':
  241. score += 0;
  242. break;
  243. default:
  244. score += parseInt(el);
  245. break;
  246. }
  247. });
  248. return score;
  249. }
  250.  
  251. //---------------------------------------
  252. // Testing Code
  253. //---------------------------------------
  254.  
  255. // EXE 1 - Property Path
  256. console.log("\nEXE 1 - Property Path\n");
  257. var object = {
  258. a: 1,
  259. b: {
  260. c: 2,
  261. d: 3
  262. }
  263. }
  264. console.log(propertyValueAt(object, ['a']));
  265. console.log(propertyValueAt(object, ['b']));
  266. console.log(propertyValueAt(object, ['b'], ['d']));
  267. console.log(propertyValueAt(object, ['d']));
  268. console.log(propertyValueAt(object, ['q']));
  269.  
  270. // EXE 2 - Sum Nested
  271. console.log("\nEXE 2 - Sum Nested\n");
  272. console.log(sumNested([1, 1, 1, [3, 4, [8]], [5]]));
  273. console.log(sumNested([]));
  274.  
  275. // EXE 3 - Word Count
  276. console.log("\nEXE 3 - Word Count\n");
  277. console.log(wordCount('This is a short sentence!'));
  278. console.log(wordCount('Thisisa$!longword!'));
  279. console.log(wordCount(' '));
  280.  
  281. // EXE 4 - Anagram
  282. console.log("\nEXE 4 - Anagram\n");
  283. console.log(areTheseAnagrams('abc', 'bca'));
  284. console.log(areTheseAnagrams('abc', 'cde'));
  285.  
  286. // EXE 5 - Analyze Prices
  287. console.log("\nEXE 5 - Analyze Prices\n");
  288. var prices = [1,2,3,4,5,6,7,8,9,0];
  289. console.log(analyzePrices(prices));
  290. prices = [8,4,3,1,5,5,2,8,2,3];
  291. console.log(analyzePrices(prices));
  292. prices = [5,2,5,4,5,6,3,8,1,8];
  293. console.log(analyzePrices(prices));
  294. prices = [1,1,1,1,1];
  295. console.log(analyzePrices(prices));
  296.  
  297. // EXE 6 - FizzBuzz
  298. console.log("\nEXE 6 - FizzBuzz\n");
  299. console.log(fizzBuzz(0));
  300. console.log(fizzBuzz(15));
  301.  
  302. // EXE 7 - Car
  303. console.log("\nEXE 7 - Car\n");
  304. var car = new Car();
  305. console.log(car.getSpeed());
  306. car.setSpeed(10);
  307. console.log(car.getSpeed());
  308. car.stop();
  309. console.log(car.getSpeed());
  310.  
  311. // BONUS 1 - Calc Bowling Scores
  312. console.log("\nBONUS 1 - Calc Bowling Scores\n");
  313. var game1 = "XXXXXXXXXX";
  314. console.log(calculateBowlingScores(game1));
  315. </script>
  316.  
  317.  
  318.  
  319. <script id="jsbin-source-javascript" type="text/javascript">//---------------------------------------
  320. // Name: Brad Francis
  321. //---------------------------------------
  322.  
  323. //---------------------------------------
  324. // Exercise: Property Path Evaluation
  325. //---------------------------------------
  326.  
  327. function propertyValueAt(object, attributes) {
  328. var vals = [];
  329. attributes.forEach(function(attr){
  330. if(Array.isArray(attr))
  331. propertValueAtRec(vals, attr, attributes);
  332. else
  333. vals.push(object[attr]);
  334. });
  335. return vals;
  336. }
  337.  
  338. function propertValueAtRec(vals, object, attributes) {
  339. attributes.forEach(function(attr){
  340. if(typeof attr === "object")
  341. propertValueAtRec(vals, attr, attributes);
  342. else
  343. vals.push(object[attr]);
  344. });
  345. return vals;
  346. }
  347.  
  348. //---------------------------------------
  349. // Exercise: Sum Nested Arrays
  350. //---------------------------------------
  351.  
  352. var sum;
  353.  
  354. function sumNested(arr) {
  355. sum = 0;
  356. sumNestedRec(arr);
  357. return sum;
  358. }
  359.  
  360. function sumNestedRec(arr) {
  361. arr.forEach(function(el){
  362. if(Array.isArray(el))
  363. sumNestedRec(el);
  364. else
  365. sum += el;
  366. });
  367. }
  368.  
  369. //---------------------------------------
  370. // Exercise: Word Count
  371. //---------------------------------------
  372.  
  373. function wordCount(sentence) {
  374.  
  375. var midWord = false;
  376. var count = 0;
  377. var letters = sentence.split("");
  378.  
  379. letters.forEach(function(char){
  380. if(char === ' ') // if space
  381. midWord = false;
  382. else if (!midWord){ // if new word
  383. count++;
  384. midWord = true;
  385. }
  386. });
  387.  
  388. return count;
  389. }
  390.  
  391. //---------------------------------------
  392. // Exercise: Anagram Tester
  393. //---------------------------------------
  394.  
  395. function areTheseAnagrams(word1, word2) {
  396. var isAnagram = false;
  397. var isInBoth = false;
  398. var word1letters = word1.split("");
  399. var word2letters = word2.split("");
  400. for(var i in word1letters) {
  401. isInBoth = false;
  402. for(var j in word2letters) {
  403. if(word1letters[i] === word2letters[j])
  404. isInBoth = true;
  405. }
  406. if(isInBoth)
  407. isAnagram = true;
  408. else
  409. return false;
  410. }
  411.  
  412. return isAnagram;
  413. }
  414.  
  415. //---------------------------------------
  416. // Exercise: Analyze Prices
  417. //---------------------------------------
  418.  
  419. function analyzePrices(prices) {
  420. var analysis = new Object();
  421. /*
  422. var buy, sell;
  423. while ((buy = calcBuyIndex(prices)) === null) {}
  424. while ((sell = calcSellIndex(prices)) === null) {}
  425. if(buy === null || sell === null) {
  426. analysis.buyIndex = null;
  427. analysis.sellIndex = null;
  428. }
  429. else {
  430. analysis.buyIndex = buy;
  431. analysis.sellIndex = sell;
  432. }
  433. */
  434.  
  435. var maxProfit = 0;
  436. var buyInd;
  437. var sellInd;
  438. var currProfit;
  439. for(var i = 0; i < prices.length; i++){
  440. for(var j = 0; j < prices.length; j++){
  441. currProfit = prices[j] - prices[i];
  442. if(currProfit > maxProfit && i < j){
  443. maxProfit = currProfit;
  444. buyInd = i;
  445. sellInd = j;
  446. }
  447. }
  448. }
  449. if (maxProfit === 0){
  450. buyInd = null;
  451. sellInd = null;
  452. }
  453. analysis.buyIndex = buyInd;
  454. analysis.sellIndex = sellInd;
  455. return analysis;
  456. }
  457.  
  458. /*
  459. function indexOfMin(arr){
  460. var minIndex = 0;
  461. for (var i in arr){
  462. if (arr[i] < arr[minIndex])
  463. minIndex = i;
  464. }
  465. }
  466.  
  467. function indexOfMax(arr){
  468. var maxIndex = 0;
  469. for (var i in arr){
  470. if (arr[i] > arr[maxIndex])
  471. maxIndex = i;
  472. }
  473. }
  474.  
  475. function calcBuyIndex(arr){
  476. var minIndex = indexOfMin(arr);
  477. var maxIndex = indexOfMax(arr);
  478. if (maxIndex <= minIndex){
  479. arr.splice(minIndex, 1);
  480. return null;
  481. }
  482. else
  483. return minIndex;
  484. }
  485.  
  486. function calcSellIndex(arr){
  487. var minIndex = indexOfMin(arr);
  488. var maxIndex = indexOfMax(arr);
  489. if (maxIndex <= minIndex){
  490. arr.splice(maxIndex, 1);
  491. return null;
  492. }
  493. else
  494. return maxIndex;
  495. }
  496. */
  497.  
  498. //---------------------------------------
  499. // Exercise: Fizz Buzz
  500. //---------------------------------------
  501.  
  502. function fizzBuzz(n) {
  503. var output = "";
  504. if (n > 0) {
  505. for (var i = 1; i <= n; i++) {
  506. output += i;
  507. if(i % 3 === 0)
  508. output += "fizz";
  509. if(i % 5 === 0)
  510. output += "buzz";
  511. if (i < n)
  512. output += ", ";
  513. }
  514. }
  515. return output;
  516. }
  517.  
  518. //---------------------------------------
  519. // Exercise: OOP - Car
  520. //---------------------------------------
  521.  
  522. class Car {
  523.  
  524. constructor(){
  525. this.speed = 0;
  526. }
  527.  
  528. getSpeed() { return this.speed; }
  529. setSpeed(speed) { this.speed = speed; }
  530. stop() { this.speed = 0; }
  531. }
  532.  
  533. //---------------------------------------
  534. // Bonus: OCalculate Bowling Score
  535. //---------------------------------------
  536.  
  537. function calculateBowlingScores(scores){
  538. var score = 0;
  539. var scoreLetters = scores.split("");
  540. scoreLetters.forEach(function(el){
  541. switch (el) {
  542. case 'X':
  543. score += 20;
  544. break;
  545. case '/':
  546. score += 10;
  547. break;
  548. case '-':
  549. score += 0;
  550. break;
  551. default:
  552. score += parseInt(el);
  553. break;
  554. }
  555. });
  556. return score;
  557. }
  558.  
  559. //---------------------------------------
  560. // Testing Code
  561. //---------------------------------------
  562.  
  563. // EXE 1 - Property Path
  564. console.log("\nEXE 1 - Property Path\n");
  565. var object = {
  566. a: 1,
  567. b: {
  568. c: 2,
  569. d: 3
  570. }
  571. }
  572. console.log(propertyValueAt(object, ['a']));
  573. console.log(propertyValueAt(object, ['b']));
  574. console.log(propertyValueAt(object, ['b'], ['d']));
  575. console.log(propertyValueAt(object, ['d']));
  576. console.log(propertyValueAt(object, ['q']));
  577.  
  578. // EXE 2 - Sum Nested
  579. console.log("\nEXE 2 - Sum Nested\n");
  580. console.log(sumNested([1, 1, 1, [3, 4, [8]], [5]]));
  581. console.log(sumNested([]));
  582.  
  583. // EXE 3 - Word Count
  584. console.log("\nEXE 3 - Word Count\n");
  585. console.log(wordCount('This is a short sentence!'));
  586. console.log(wordCount('Thisisa$!longword!'));
  587. console.log(wordCount(' '));
  588.  
  589. // EXE 4 - Anagram
  590. console.log("\nEXE 4 - Anagram\n");
  591. console.log(areTheseAnagrams('abc', 'bca'));
  592. console.log(areTheseAnagrams('abc', 'cde'));
  593.  
  594. // EXE 5 - Analyze Prices
  595. console.log("\nEXE 5 - Analyze Prices\n");
  596. var prices = [1,2,3,4,5,6,7,8,9,0];
  597. console.log(analyzePrices(prices));
  598. prices = [8,4,3,1,5,5,2,8,2,3];
  599. console.log(analyzePrices(prices));
  600. prices = [5,2,5,4,5,6,3,8,1,8];
  601. console.log(analyzePrices(prices));
  602. prices = [1,1,1,1,1];
  603. console.log(analyzePrices(prices));
  604.  
  605. // EXE 6 - FizzBuzz
  606. console.log("\nEXE 6 - FizzBuzz\n");
  607. console.log(fizzBuzz(0));
  608. console.log(fizzBuzz(15));
  609.  
  610. // EXE 7 - Car
  611. console.log("\nEXE 7 - Car\n");
  612. var car = new Car();
  613. console.log(car.getSpeed());
  614. car.setSpeed(10);
  615. console.log(car.getSpeed());
  616. car.stop();
  617. console.log(car.getSpeed());
  618.  
  619. // BONUS 1 - Calc Bowling Scores
  620. console.log("\nBONUS 1 - Calc Bowling Scores\n");
  621. var game1 = "XXXXXXXXXX";
  622. console.log(calculateBowlingScores(game1));
  623.  
  624.  
  625.  
  626.  
  627.  
  628.  
  629.  
  630.  
  631.  
  632. </script></body>
  633. </html>
Add Comment
Please, Sign In to add comment