Guest User

Untitled

a guest
Jan 21st, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.85 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: Christopher Cobb
  13. // --------------------------------------------------------------------------
  14.  
  15. // --------------------------------------------------------------------------
  16. // Exercise: Property Path Evaluation
  17. // --------------------------------------------------------------------------
  18.  
  19. //Function propertyValueAt
  20. //Input: object whose value to find.
  21. // propertys: an array of a property to find.
  22. //Output: object's property based on input array, or undefined if not found.
  23. function propertyValueAt(inputObject,propertys){
  24. //We need to find the property, which could be another object of properties.
  25. var propertyValue = inputObject;
  26. //Let's loop through propertys to go down the list.
  27. for(var i = 0; i < propertys.length; i++ ){
  28. propertyValue = propertyValue[propertys[i]];
  29. }
  30. return propertyValue;
  31. }
  32. /*
  33. var object = {
  34. a: 1,
  35. b: {
  36. c: 2,
  37. d: 3
  38. }
  39. };
  40.  
  41. console.log(propertyValueAt(object,['b','f']));
  42. */
  43. // --------------------------------------------------------------------------
  44. // Name: Christopher Cobb
  45. // --------------------------------------------------------------------------
  46.  
  47. // --------------------------------------------------------------------------
  48. // Exercise: Sum Nested Arrays
  49. // --------------------------------------------------------------------------
  50.  
  51. // Function sumNested
  52. // Input: addArray - an array of numbers and arrays of numbers to be added.
  53. // Return Output: The sum of all numbers (current code version has to have numbers)
  54.  
  55. //var maxLoop = 0; //Inf. loop protector cuz JSBin doesn't have a stop option.
  56. function sumNested(addArray){
  57. //Let's go for this approach:
  58. //for each element, we need to recurse on each element of the array./*
  59. /*maxLoop++;
  60. if( maxLoop > 1000 ){
  61. console.log("max hit");
  62. return 0;
  63. }*/
  64. var sum = 0;
  65. //So if we're just a number at a array element, add it up!
  66. // else dive into the array until we're at the elements then add them up.
  67.  
  68. for( var i = 0; i < addArray.length; i++){
  69. if(!Array.isArray(addArray[i])){
  70. console.log('Adding ' + addArray[i]);
  71. sum = sum + addArray[i]; // Single num found! Add it up!
  72. } else {
  73. console.log('recursive ' + addArray[i]);
  74. sum = sum + sumNested(addArray[i]); //Add up everything inside the array!
  75. }
  76. console.log("Sum:" + sum);
  77. }
  78. return sum;
  79. }
  80.  
  81. //console.log(sumNested([])); //23
  82.  
  83. // --------------------------------------------------------------------------
  84. // Name: Christopher Cobb
  85. // --------------------------------------------------------------------------
  86.  
  87. // --------------------------------------------------------------------------
  88. // Exercise: Word Count
  89. // --------------------------------------------------------------------------
  90.  
  91. //Function wordCount(sentence)
  92. // Input: A string of characters to count the # of words of.
  93. // Output: The number of words. Words are space seperated, and can consist
  94. // of any type of symbol that isn't a space or newline.
  95. function wordCount(sentence){
  96. //A simple problem, simply check for spaces in a small loop
  97. var numWords = 0;
  98. var atSpace = true;
  99. //Here's the logic. We start assuming we're at a space.
  100. //Check a letter, if we're at a space and the next letter is a real letter
  101. // we're at a new word!
  102. //If we're not at a space, then we end the current word at a space, then
  103. // check for the next word as above.
  104. for( var i = 0; i < sentence.length; i++){
  105. if(atSpace){
  106. if(sentence[i] !== ' ' && sentence[i] !== '\n'){
  107. atSpace = false;
  108. numWords++;
  109. }
  110. } else {
  111. atSpace = sentence[i] === ' ' || sentence[i] === '\n';
  112. }
  113. }
  114. return numWords;
  115. }
  116.  
  117. //console.log(wordCount('Its a newline\ntest! \n Hi!'));
  118. //Returns 5, "Its" "a" "newline" "test!" "Hi!"
  119.  
  120. // --------------------------------------------------------------------------
  121. // Name: Christopher Cobb
  122. // --------------------------------------------------------------------------
  123.  
  124. // --------------------------------------------------------------------------
  125. // Exercise: Anagram Tester
  126. // --------------------------------------------------------------------------
  127.  
  128. //function isNumberofEqual(testChar, stringA, stringB)
  129. //input: a character to test for the number of occurances
  130. // string 1 to check.
  131. // string 2 to check.
  132. //output: If the number of testCha occurances in the strings is the same
  133.  
  134. //function areTheseAnagrams(stringA,stringB)
  135. //input: stringa and stringb to check for anagrams.
  136. //output: if they are anagrams.
  137. function isNumberOfEqual(testChar, stringA ,stringB){
  138. var numA = 0;
  139. var numB = 0;
  140. //Let's count all the characters!
  141. for( var i = 0; i < stringA.length; i++){
  142. if(stringA.charAt(i) === testChar){
  143. numA++;
  144. }
  145. if(stringB.charAt(i) === testChar ){
  146. numB++;
  147. }
  148. }
  149. return numA === numB;
  150. }
  151.  
  152. function areTheseAnagrams(stringA,stringB){
  153. //Ok! We got two string! First, if they're not equal length they're not anas
  154. if( stringA.length != stringB.length)
  155. {
  156. return false;
  157. }
  158. //Now from here on they are equal length. My quick idea is a markoff method.
  159. //is to check if the character at i ocurs the same amount of times in both
  160. // strings, using isNumberofEqual.
  161. for(var i = 0; i < stringA.length; i++){
  162. //if(stringB.indexOf(stringA.charAt(i)) === -1){
  163. if( isNumberOfEqual(stringA.charAt(i),stringA,stringB) === false ){
  164. return false;
  165. }
  166. }
  167. return true;
  168. }
  169.  
  170.  
  171. //console.log(areTheseAnagrams("abc","cde"));
  172.  
  173. // --------------------------------------------------------------------------
  174. // Name: Christopher Cobb
  175. // --------------------------------------------------------------------------
  176.  
  177. // --------------------------------------------------------------------------
  178. // Exercise: Analyze Prices
  179. // --------------------------------------------------------------------------
  180.  
  181. //function analyzePrices(priceArray)
  182. //input: An array of prices, indexed by time.
  183. //output: an object of buyIndex: the index to buy the stock. sellIndex: the index to sell
  184. //Nulls if it's not profitable at all.
  185. function analyzePrices(priceArray){
  186. var priceIndex = new Object;
  187. priceIndex.buyIndex = 0;
  188. priceIndex.sellIndex = 0;
  189. var maxProfit = 0;
  190. var heldStock = 0;
  191. for( var i = 0; i < priceArray.length; i++){
  192. for( var j = 1; j < priceArray.length; j++){
  193. //find the profit for buying at i, selling at j
  194. if(i < j){ //can't sell before buy
  195. var profit = priceArray[j]-priceArray[i];
  196. if(profit > maxProfit){
  197. //record!
  198. priceIndex.buyIndex = i;
  199. priceIndex.sellIndex = j;
  200. maxProfit = profit;
  201. }
  202. }
  203. }
  204. }
  205. if( maxProfit === 0){
  206. priceIndex.buyIndex = null;
  207. priceIndex.sellIndex = null;
  208. }
  209. return priceIndex;
  210. }
  211. //console.log(analyzePrices([1,1,1,1,1]));
  212.  
  213. // --------------------------------------------------------------------------
  214. // Name: Christopher Cobb
  215. // --------------------------------------------------------------------------
  216.  
  217. // --------------------------------------------------------------------------
  218. // Exercise: Fizz Buzz
  219. // --------------------------------------------------------------------------
  220.  
  221. //function fizzBuzz(n)
  222. //input: A single number, n, to list out with 3's for fizes and 5 for buzzes.
  223. //output: a string of characters from 1 to n, with commas, and fizz's if divis.3 and
  224. //buzzes if div by 5. Both fizzbuzz if both.
  225.  
  226. function fizzBuzz(n){
  227. var returnString = "";
  228. if( n <= 0 ){
  229. return returnString;
  230. }
  231. for( var i = 1; i <= n; i++){
  232. returnString += i;
  233. if(i%3 === 0){
  234. returnString += 'fizz';
  235. }
  236.  
  237. if(i%5 === 0){
  238. returnString += 'buzz';
  239. }
  240. if( i != n){
  241. returnString += ","
  242. }
  243. }
  244.  
  245. return returnString;
  246. }
  247.  
  248.  
  249. //Really? Fizzbuzz?
  250. //console.log(fizzBuzz(15));
  251.  
  252. // --------------------------------------------------------------------------
  253. // Name: Christopher Cobb
  254. // --------------------------------------------------------------------------
  255.  
  256. // --------------------------------------------------------------------------
  257. // Exercise: Object Oriented Programming - Car
  258. // --------------------------------------------------------------------------
  259.  
  260. //function car(){
  261. //this.speed = 0;
  262. // this.getSpeed = function() {return this.speed;}
  263. // this.setSpeed = function(newSpeed) {this.speed = newSpeed;}
  264. // this.stop = function() {this.speed = 0; }
  265. //}
  266.  
  267.  
  268.  
  269. }
  270. </script>
  271.  
  272.  
  273.  
  274. <script id="jsbin-source-javascript" type="text/javascript">// --------------------------------------------------------------------------
  275. // Name: Christopher Cobb
  276. // --------------------------------------------------------------------------
  277.  
  278. // --------------------------------------------------------------------------
  279. // Exercise: Property Path Evaluation
  280. // --------------------------------------------------------------------------
  281.  
  282. //Function propertyValueAt
  283. //Input: object whose value to find.
  284. // propertys: an array of a property to find.
  285. //Output: object's property based on input array, or undefined if not found.
  286. function propertyValueAt(inputObject,propertys){
  287. //We need to find the property, which could be another object of properties.
  288. var propertyValue = inputObject;
  289. //Let's loop through propertys to go down the list.
  290. for(var i = 0; i < propertys.length; i++ ){
  291. propertyValue = propertyValue[propertys[i]];
  292. }
  293. return propertyValue;
  294. }
  295. /*
  296. var object = {
  297. a: 1,
  298. b: {
  299. c: 2,
  300. d: 3
  301. }
  302. };
  303.  
  304. console.log(propertyValueAt(object,['b','f']));
  305. */
  306. // --------------------------------------------------------------------------
  307. // Name: Christopher Cobb
  308. // --------------------------------------------------------------------------
  309.  
  310. // --------------------------------------------------------------------------
  311. // Exercise: Sum Nested Arrays
  312. // --------------------------------------------------------------------------
  313.  
  314. // Function sumNested
  315. // Input: addArray - an array of numbers and arrays of numbers to be added.
  316. // Return Output: The sum of all numbers (current code version has to have numbers)
  317.  
  318. //var maxLoop = 0; //Inf. loop protector cuz JSBin doesn't have a stop option.
  319. function sumNested(addArray){
  320. //Let's go for this approach:
  321. //for each element, we need to recurse on each element of the array./*
  322. /*maxLoop++;
  323. if( maxLoop > 1000 ){
  324. console.log("max hit");
  325. return 0;
  326. }*/
  327. var sum = 0;
  328. //So if we're just a number at a array element, add it up!
  329. // else dive into the array until we're at the elements then add them up.
  330.  
  331. for( var i = 0; i < addArray.length; i++){
  332. if(!Array.isArray(addArray[i])){
  333. console.log('Adding ' + addArray[i]);
  334. sum = sum + addArray[i]; // Single num found! Add it up!
  335. } else {
  336. console.log('recursive ' + addArray[i]);
  337. sum = sum + sumNested(addArray[i]); //Add up everything inside the array!
  338. }
  339. console.log("Sum:" + sum);
  340. }
  341. return sum;
  342. }
  343.  
  344. //console.log(sumNested([])); //23
  345.  
  346. // --------------------------------------------------------------------------
  347. // Name: Christopher Cobb
  348. // --------------------------------------------------------------------------
  349.  
  350. // --------------------------------------------------------------------------
  351. // Exercise: Word Count
  352. // --------------------------------------------------------------------------
  353.  
  354. //Function wordCount(sentence)
  355. // Input: A string of characters to count the # of words of.
  356. // Output: The number of words. Words are space seperated, and can consist
  357. // of any type of symbol that isn't a space or newline.
  358. function wordCount(sentence){
  359. //A simple problem, simply check for spaces in a small loop
  360. var numWords = 0;
  361. var atSpace = true;
  362. //Here's the logic. We start assuming we're at a space.
  363. //Check a letter, if we're at a space and the next letter is a real letter
  364. // we're at a new word!
  365. //If we're not at a space, then we end the current word at a space, then
  366. // check for the next word as above.
  367. for( var i = 0; i < sentence.length; i++){
  368. if(atSpace){
  369. if(sentence[i] !== ' ' && sentence[i] !== '\n'){
  370. atSpace = false;
  371. numWords++;
  372. }
  373. } else {
  374. atSpace = sentence[i] === ' ' || sentence[i] === '\n';
  375. }
  376. }
  377. return numWords;
  378. }
  379.  
  380. //console.log(wordCount('Its a newline\ntest! \n Hi!'));
  381. //Returns 5, "Its" "a" "newline" "test!" "Hi!"
  382.  
  383. // --------------------------------------------------------------------------
  384. // Name: Christopher Cobb
  385. // --------------------------------------------------------------------------
  386.  
  387. // --------------------------------------------------------------------------
  388. // Exercise: Anagram Tester
  389. // --------------------------------------------------------------------------
  390.  
  391. //function isNumberofEqual(testChar, stringA, stringB)
  392. //input: a character to test for the number of occurances
  393. // string 1 to check.
  394. // string 2 to check.
  395. //output: If the number of testCha occurances in the strings is the same
  396.  
  397. //function areTheseAnagrams(stringA,stringB)
  398. //input: stringa and stringb to check for anagrams.
  399. //output: if they are anagrams.
  400. function isNumberOfEqual(testChar, stringA ,stringB){
  401. var numA = 0;
  402. var numB = 0;
  403. //Let's count all the characters!
  404. for( var i = 0; i < stringA.length; i++){
  405. if(stringA.charAt(i) === testChar){
  406. numA++;
  407. }
  408. if(stringB.charAt(i) === testChar ){
  409. numB++;
  410. }
  411. }
  412. return numA === numB;
  413. }
  414.  
  415. function areTheseAnagrams(stringA,stringB){
  416. //Ok! We got two string! First, if they're not equal length they're not anas
  417. if( stringA.length != stringB.length)
  418. {
  419. return false;
  420. }
  421. //Now from here on they are equal length. My quick idea is a markoff method.
  422. //is to check if the character at i ocurs the same amount of times in both
  423. // strings, using isNumberofEqual.
  424. for(var i = 0; i < stringA.length; i++){
  425. //if(stringB.indexOf(stringA.charAt(i)) === -1){
  426. if( isNumberOfEqual(stringA.charAt(i),stringA,stringB) === false ){
  427. return false;
  428. }
  429. }
  430. return true;
  431. }
  432.  
  433.  
  434. //console.log(areTheseAnagrams("abc","cde"));
  435.  
  436. // --------------------------------------------------------------------------
  437. // Name: Christopher Cobb
  438. // --------------------------------------------------------------------------
  439.  
  440. // --------------------------------------------------------------------------
  441. // Exercise: Analyze Prices
  442. // --------------------------------------------------------------------------
  443.  
  444. //function analyzePrices(priceArray)
  445. //input: An array of prices, indexed by time.
  446. //output: an object of buyIndex: the index to buy the stock. sellIndex: the index to sell
  447. //Nulls if it's not profitable at all.
  448. function analyzePrices(priceArray){
  449. var priceIndex = new Object;
  450. priceIndex.buyIndex = 0;
  451. priceIndex.sellIndex = 0;
  452. var maxProfit = 0;
  453. var heldStock = 0;
  454. for( var i = 0; i < priceArray.length; i++){
  455. for( var j = 1; j < priceArray.length; j++){
  456. //find the profit for buying at i, selling at j
  457. if(i < j){ //can't sell before buy
  458. var profit = priceArray[j]-priceArray[i];
  459. if(profit > maxProfit){
  460. //record!
  461. priceIndex.buyIndex = i;
  462. priceIndex.sellIndex = j;
  463. maxProfit = profit;
  464. }
  465. }
  466. }
  467. }
  468. if( maxProfit === 0){
  469. priceIndex.buyIndex = null;
  470. priceIndex.sellIndex = null;
  471. }
  472. return priceIndex;
  473. }
  474. //console.log(analyzePrices([1,1,1,1,1]));
  475.  
  476. // --------------------------------------------------------------------------
  477. // Name: Christopher Cobb
  478. // --------------------------------------------------------------------------
  479.  
  480. // --------------------------------------------------------------------------
  481. // Exercise: Fizz Buzz
  482. // --------------------------------------------------------------------------
  483.  
  484. //function fizzBuzz(n)
  485. //input: A single number, n, to list out with 3's for fizes and 5 for buzzes.
  486. //output: a string of characters from 1 to n, with commas, and fizz's if divis.3 and
  487. //buzzes if div by 5. Both fizzbuzz if both.
  488.  
  489. function fizzBuzz(n){
  490. var returnString = "";
  491. if( n <= 0 ){
  492. return returnString;
  493. }
  494. for( var i = 1; i <= n; i++){
  495. returnString += i;
  496. if(i%3 === 0){
  497. returnString += 'fizz';
  498. }
  499.  
  500. if(i%5 === 0){
  501. returnString += 'buzz';
  502. }
  503. if( i != n){
  504. returnString += ","
  505. }
  506. }
  507.  
  508. return returnString;
  509. }
  510.  
  511.  
  512. //Really? Fizzbuzz?
  513. //console.log(fizzBuzz(15));
  514.  
  515. // --------------------------------------------------------------------------
  516. // Name: Christopher Cobb
  517. // --------------------------------------------------------------------------
  518.  
  519. // --------------------------------------------------------------------------
  520. // Exercise: Object Oriented Programming - Car
  521. // --------------------------------------------------------------------------
  522.  
  523. //function car(){
  524. //this.speed = 0;
  525. // this.getSpeed = function() {return this.speed;}
  526. // this.setSpeed = function(newSpeed) {this.speed = newSpeed;}
  527. // this.stop = function() {this.speed = 0; }
  528. //}
  529.  
  530.  
  531.  
  532. }</script></body>
  533. </html>
Add Comment
Please, Sign In to add comment