Guest User

Untitled

a guest
Jan 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.69 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. // Sean Gibbons
  13. // ------------------------------------------------------------
  14.  
  15. // ------------------------------------------------------------
  16. // Exercise: Property Path Evaluation
  17. // ------------------------------------------------------------
  18.  
  19. function propertyValueAt(object, propertyArray){
  20. if (object.hasOwnProperty(propertyArray[0])){ //if the property exists in the object
  21. if ((propertyArray.length > 1)){ //if there is another property to search for within this object property
  22. console.log(object[propertyArray[0]]);
  23. console.log(typeof object[propertyArray[0]]);
  24. if (typeof object[propertyArray[0]] === object){ //if this property is an object, and thus can hold the next property
  25. var nextArray = propertyArray;
  26. nextArray.splice(0, 1);
  27. propertyValueAt(object[propertyArray[0]], nextArray); //perform the function again with that object property and that next property
  28. }
  29. else{ //if this property is not an object and thus the next property cannot be within it
  30. return undefined;
  31. }
  32. }
  33. else{ //if that is the last property
  34. return object[propertyArray[0]]; //return that property's value
  35. }
  36. }
  37. else{
  38. return undefined; //otherwise return undefined
  39. }
  40. }
  41.  
  42. var object1 = {
  43. a: 1,
  44. b: {
  45. c: 2,
  46. d: {
  47. e: 3,
  48. f: 4
  49. }
  50. }
  51. }
  52.  
  53. console.log('propertyValueAt test');
  54. console.log(propertyValueAt(object1, ['a']));
  55. console.log(propertyValueAt(object1, ['a', 'b']));
  56. console.log(propertyValueAt(object1, ['b', 'c']));
  57. console.log(propertyValueAt(object1, ['b', 'd']));
  58. console.log(propertyValueAt(object1, ['b', 'd', 'e']));
  59. console.log(propertyValueAt(object1, []));
  60.  
  61. // ------------------------------------------------------------
  62. // Exercise: Sum Nested Arrays
  63. // ------------------------------------------------------------
  64.  
  65. function sumNested(array){
  66. var sum = 0; //sum to be returned
  67. for (var i = 0; i < array.length; i++){ //for each element of the array
  68. if (typeof array[i] === 'object'){ //if this element is an array itself
  69. sum += sumNested(array[i]); //add that array's sum to the sum
  70. }
  71. else{ //if this element is a number
  72. sum += array[i]; //add it to the sum
  73. }
  74. }
  75. return sum;
  76. }
  77.  
  78. console.log('sumNested test')
  79. console.log(sumNested([]));
  80. console.log(sumNested([1]));
  81. console.log(sumNested([1, 2]));
  82. console.log(sumNested([1, [2, 3]]));
  83. console.log(sumNested([1, 1, 1, 1, [2, [3, 4]]]));
  84.  
  85. // ------------------------------------------------------------
  86. // Exercise: Word Count
  87. // ------------------------------------------------------------
  88.  
  89. function wordCount(sentence){
  90. var count = 0; //return value
  91. var nonSpace = new RegExp("[^\s]"); //regex for non white space
  92. if (nonSpace.test(sentence) === true){ //if the sentence contains characters other than white space
  93. var sentenceSplit = sentence.split(" "); //split the words up
  94. for (var i in sentenceSplit){
  95. count++; //for each word, increment the count
  96. }
  97. }
  98. else{ //if there were no words, return 0
  99. return 0;
  100. }
  101. if (sentence.endsWith(' ')){ //if the sentence ends with white space, decrement count so it doesn't include the empty string at the end
  102. return count - 1;
  103. }
  104. else{
  105. return count;
  106. }
  107.  
  108. }
  109.  
  110. console.log('wordCount test');
  111. console.log(wordCount(""));
  112. console.log(wordCount("a a"));
  113. console.log(wordCount("7vfg*"));
  114. console.log(wordCount("aa aa "));
  115. console.log(wordCount('aa aa aa'));
  116.  
  117. // ------------------------------------------------------------
  118. // Exercise: Anagram Tester
  119. // ------------------------------------------------------------
  120.  
  121. function areTheseAnagrams(str1, str2){
  122. var anagramTruth = true; //return value
  123. str2 = str2.split(""); //split string 2 into an array
  124. for (var i in str1){ //for each character in string 1
  125. if (str2.includes(str1[i])){ //if string 2 has that character
  126. charIndex = str2.indexOf(str1[i]); //find the index of the first instance of that character
  127. str2.splice(charIndex, 1); //remove the character so that it doesn't get counted again
  128. }
  129. else{
  130. anagramTruth = false;
  131. }
  132. }
  133. return anagramTruth;
  134. }
  135.  
  136. console.log('areTheseAnagrams test')
  137. console.log(areTheseAnagrams("",""));
  138. console.log(areTheseAnagrams("a","a"));
  139. console.log(areTheseAnagrams("a","b"));
  140. console.log(areTheseAnagrams("aab","bab"));
  141. console.log(areTheseAnagrams("abc","cab"));
  142.  
  143. // ------------------------------------------------------------
  144. // Exercise: Analyze Prices
  145. // ------------------------------------------------------------
  146.  
  147. function analyzePrices(prices){
  148. var buyIndex = null;
  149. var sellIndex = null;
  150. var sellArray = [];
  151. var profit = 0; //max profit
  152.  
  153. for (var i = 0; i < prices.length; i++){
  154. sellArray = prices.slice(i); //sell array is the array of numbers you can sell for after buying at a certain index
  155. if (sellArray[0] !== Math.min(sellArray)){ //if the buy point is not the lowest number
  156. if (Math.min(sellArray) - sellArray[0] > profit){ //if the profit between the buy point and the best sell point is the max profit
  157. profit = Math.min(sellArray) - sellArray[0]; //the max profit is changed to be correct
  158. buyIndex = i; //the buy index is the current buy point
  159. sellIndex = sellArray.indexOf(Math.min(sellArray)); //the sell index is the best sell point
  160. }
  161. }
  162. }
  163.  
  164. var answer = {
  165. 'buyIndex': buyIndex,
  166. 'sellIndex': sellIndex
  167. }; //object to be returned
  168. return answer;
  169. }
  170.  
  171. console.log('analyzePrices test');
  172. console.log(analyzePrices([1,2,3,4,5,6,7,8,9,0]));
  173. console.log(analyzePrices([8,4,3,1,5,5,2,8,2,3]));
  174. console.log(analyzePrices([5,2,5,4,5,6,3,8,1,8]));
  175. console.log(analyzePrices([1,1,1,1,1,1,1]));
  176.  
  177. // ------------------------------------------------------------
  178. // Exercise: Fizz Buzz
  179. // ------------------------------------------------------------
  180.  
  181. function fizzBuzz(n){
  182. if (n <= 0){
  183. return "";
  184. }
  185. else{
  186. var answer = ""; //return string
  187. for (i = 1; i <= n; i++){ //for numbers between 1 and n
  188. if ((i % 3 === 0) && (i % 5 === 0)){ //if the number is divisible by both 3 and 5
  189. answer += i + 'fizzbuzz'; //add the number and fizzbuzz to the screen
  190. }
  191. else if(i % 3 === 0){ //if divisible by 3
  192. answer += i + 'fizz';
  193. }
  194. else if(i % 5 === 0){ //if divisible by 5
  195. answer += i + 'buzz';
  196. }
  197. else{
  198. answer += i;
  199. }
  200. if (i !== n){ //if this isn't the last number
  201. answer += ", "; //ass a comma and a space
  202. }
  203. }
  204. return answer;
  205. }
  206. }
  207.  
  208. console.log('fizzBuzz test');
  209. console.log(fizzBuzz(0));
  210. console.log(fizzBuzz(3));
  211. console.log(fizzBuzz(5));
  212. console.log(fizzBuzz(15));
  213.  
  214. // ------------------------------------------------------------
  215. // Exercise: Object Oriented Programming - Car
  216. // ------------------------------------------------------------
  217.  
  218. class Car {
  219. constructor(speed){
  220. if (speed !== undefined){
  221. this._speed = speed;
  222. }
  223. else{
  224. this._speed = 0;
  225. }
  226. }
  227.  
  228. get getSpeed(){
  229. return this._speed;
  230. }
  231.  
  232. setSpeed(speed){
  233. this._speed = speed;
  234. }
  235.  
  236. stop(){
  237. this._speed = 0;
  238. }
  239. }
  240.  
  241. console.log('Car test');
  242. var car = new Car();
  243. console.log(car.getSpeed);
  244. car.setSpeed(10);
  245. console.log(car.getSpeed);
  246. car.stop();
  247. console.log(car.getSpeed);
  248. </script>
  249.  
  250.  
  251.  
  252. <script id="jsbin-source-javascript" type="text/javascript">// ------------------------------------------------------------
  253. // Sean Gibbons
  254. // ------------------------------------------------------------
  255.  
  256. // ------------------------------------------------------------
  257. // Exercise: Property Path Evaluation
  258. // ------------------------------------------------------------
  259.  
  260. function propertyValueAt(object, propertyArray){
  261. if (object.hasOwnProperty(propertyArray[0])){ //if the property exists in the object
  262. if ((propertyArray.length > 1)){ //if there is another property to search for within this object property
  263. console.log(object[propertyArray[0]]);
  264. console.log(typeof object[propertyArray[0]]);
  265. if (typeof object[propertyArray[0]] === object){ //if this property is an object, and thus can hold the next property
  266. var nextArray = propertyArray;
  267. nextArray.splice(0, 1);
  268. propertyValueAt(object[propertyArray[0]], nextArray); //perform the function again with that object property and that next property
  269. }
  270. else{ //if this property is not an object and thus the next property cannot be within it
  271. return undefined;
  272. }
  273. }
  274. else{ //if that is the last property
  275. return object[propertyArray[0]]; //return that property's value
  276. }
  277. }
  278. else{
  279. return undefined; //otherwise return undefined
  280. }
  281. }
  282.  
  283. var object1 = {
  284. a: 1,
  285. b: {
  286. c: 2,
  287. d: {
  288. e: 3,
  289. f: 4
  290. }
  291. }
  292. }
  293.  
  294. console.log('propertyValueAt test');
  295. console.log(propertyValueAt(object1, ['a']));
  296. console.log(propertyValueAt(object1, ['a', 'b']));
  297. console.log(propertyValueAt(object1, ['b', 'c']));
  298. console.log(propertyValueAt(object1, ['b', 'd']));
  299. console.log(propertyValueAt(object1, ['b', 'd', 'e']));
  300. console.log(propertyValueAt(object1, []));
  301.  
  302. // ------------------------------------------------------------
  303. // Exercise: Sum Nested Arrays
  304. // ------------------------------------------------------------
  305.  
  306. function sumNested(array){
  307. var sum = 0; //sum to be returned
  308. for (var i = 0; i < array.length; i++){ //for each element of the array
  309. if (typeof array[i] === 'object'){ //if this element is an array itself
  310. sum += sumNested(array[i]); //add that array's sum to the sum
  311. }
  312. else{ //if this element is a number
  313. sum += array[i]; //add it to the sum
  314. }
  315. }
  316. return sum;
  317. }
  318.  
  319. console.log('sumNested test')
  320. console.log(sumNested([]));
  321. console.log(sumNested([1]));
  322. console.log(sumNested([1, 2]));
  323. console.log(sumNested([1, [2, 3]]));
  324. console.log(sumNested([1, 1, 1, 1, [2, [3, 4]]]));
  325.  
  326. // ------------------------------------------------------------
  327. // Exercise: Word Count
  328. // ------------------------------------------------------------
  329.  
  330. function wordCount(sentence){
  331. var count = 0; //return value
  332. var nonSpace = new RegExp("[^\s]"); //regex for non white space
  333. if (nonSpace.test(sentence) === true){ //if the sentence contains characters other than white space
  334. var sentenceSplit = sentence.split(" "); //split the words up
  335. for (var i in sentenceSplit){
  336. count++; //for each word, increment the count
  337. }
  338. }
  339. else{ //if there were no words, return 0
  340. return 0;
  341. }
  342. if (sentence.endsWith(' ')){ //if the sentence ends with white space, decrement count so it doesn't include the empty string at the end
  343. return count - 1;
  344. }
  345. else{
  346. return count;
  347. }
  348.  
  349. }
  350.  
  351. console.log('wordCount test');
  352. console.log(wordCount(""));
  353. console.log(wordCount("a a"));
  354. console.log(wordCount("7vfg*"));
  355. console.log(wordCount("aa aa "));
  356. console.log(wordCount('aa aa aa'));
  357.  
  358. // ------------------------------------------------------------
  359. // Exercise: Anagram Tester
  360. // ------------------------------------------------------------
  361.  
  362. function areTheseAnagrams(str1, str2){
  363. var anagramTruth = true; //return value
  364. str2 = str2.split(""); //split string 2 into an array
  365. for (var i in str1){ //for each character in string 1
  366. if (str2.includes(str1[i])){ //if string 2 has that character
  367. charIndex = str2.indexOf(str1[i]); //find the index of the first instance of that character
  368. str2.splice(charIndex, 1); //remove the character so that it doesn't get counted again
  369. }
  370. else{
  371. anagramTruth = false;
  372. }
  373. }
  374. return anagramTruth;
  375. }
  376.  
  377. console.log('areTheseAnagrams test')
  378. console.log(areTheseAnagrams("",""));
  379. console.log(areTheseAnagrams("a","a"));
  380. console.log(areTheseAnagrams("a","b"));
  381. console.log(areTheseAnagrams("aab","bab"));
  382. console.log(areTheseAnagrams("abc","cab"));
  383.  
  384. // ------------------------------------------------------------
  385. // Exercise: Analyze Prices
  386. // ------------------------------------------------------------
  387.  
  388. function analyzePrices(prices){
  389. var buyIndex = null;
  390. var sellIndex = null;
  391. var sellArray = [];
  392. var profit = 0; //max profit
  393.  
  394. for (var i = 0; i < prices.length; i++){
  395. sellArray = prices.slice(i); //sell array is the array of numbers you can sell for after buying at a certain index
  396. if (sellArray[0] !== Math.min(sellArray)){ //if the buy point is not the lowest number
  397. if (Math.min(sellArray) - sellArray[0] > profit){ //if the profit between the buy point and the best sell point is the max profit
  398. profit = Math.min(sellArray) - sellArray[0]; //the max profit is changed to be correct
  399. buyIndex = i; //the buy index is the current buy point
  400. sellIndex = sellArray.indexOf(Math.min(sellArray)); //the sell index is the best sell point
  401. }
  402. }
  403. }
  404.  
  405. var answer = {
  406. 'buyIndex': buyIndex,
  407. 'sellIndex': sellIndex
  408. }; //object to be returned
  409. return answer;
  410. }
  411.  
  412. console.log('analyzePrices test');
  413. console.log(analyzePrices([1,2,3,4,5,6,7,8,9,0]));
  414. console.log(analyzePrices([8,4,3,1,5,5,2,8,2,3]));
  415. console.log(analyzePrices([5,2,5,4,5,6,3,8,1,8]));
  416. console.log(analyzePrices([1,1,1,1,1,1,1]));
  417.  
  418. // ------------------------------------------------------------
  419. // Exercise: Fizz Buzz
  420. // ------------------------------------------------------------
  421.  
  422. function fizzBuzz(n){
  423. if (n <= 0){
  424. return "";
  425. }
  426. else{
  427. var answer = ""; //return string
  428. for (i = 1; i <= n; i++){ //for numbers between 1 and n
  429. if ((i % 3 === 0) && (i % 5 === 0)){ //if the number is divisible by both 3 and 5
  430. answer += i + 'fizzbuzz'; //add the number and fizzbuzz to the screen
  431. }
  432. else if(i % 3 === 0){ //if divisible by 3
  433. answer += i + 'fizz';
  434. }
  435. else if(i % 5 === 0){ //if divisible by 5
  436. answer += i + 'buzz';
  437. }
  438. else{
  439. answer += i;
  440. }
  441. if (i !== n){ //if this isn't the last number
  442. answer += ", "; //ass a comma and a space
  443. }
  444. }
  445. return answer;
  446. }
  447. }
  448.  
  449. console.log('fizzBuzz test');
  450. console.log(fizzBuzz(0));
  451. console.log(fizzBuzz(3));
  452. console.log(fizzBuzz(5));
  453. console.log(fizzBuzz(15));
  454.  
  455. // ------------------------------------------------------------
  456. // Exercise: Object Oriented Programming - Car
  457. // ------------------------------------------------------------
  458.  
  459. class Car {
  460. constructor(speed){
  461. if (speed !== undefined){
  462. this._speed = speed;
  463. }
  464. else{
  465. this._speed = 0;
  466. }
  467. }
  468.  
  469. get getSpeed(){
  470. return this._speed;
  471. }
  472.  
  473. setSpeed(speed){
  474. this._speed = speed;
  475. }
  476.  
  477. stop(){
  478. this._speed = 0;
  479. }
  480. }
  481.  
  482. console.log('Car test');
  483. var car = new Car();
  484. console.log(car.getSpeed);
  485. car.setSpeed(10);
  486. console.log(car.getSpeed);
  487. car.stop();
  488. console.log(car.getSpeed);
  489.  
  490. </script></body>
  491. </html>
Add Comment
Please, Sign In to add comment