Guest User

Untitled

a guest
Jan 21st, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.20 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. var returnString = "";
  223. if( n <= 0 ){
  224. return returnString;
  225. }
  226. for( var i = 1; i <= n; i++){
  227. returnString += 1;
  228. if(i%3 === 0){
  229. returnString += 'fizz';
  230. }
  231.  
  232. if(i%5 === 0){
  233. returnString += 'buzz';
  234. }
  235. returnString += ","
  236.  
  237.  
  238.  
  239. }
  240.  
  241. return returnString;
  242. }
  243.  
  244.  
  245. //Really? Fizzbuzz?
  246. console.log(fizzBuzz(15));
  247. </script>
  248.  
  249.  
  250.  
  251. <script id="jsbin-source-javascript" type="text/javascript">// --------------------------------------------------------------------------
  252. // Name: Christopher Cobb
  253. // --------------------------------------------------------------------------
  254.  
  255. // --------------------------------------------------------------------------
  256. // Exercise: Property Path Evaluation
  257. // --------------------------------------------------------------------------
  258.  
  259. //Function propertyValueAt
  260. //Input: object whose value to find.
  261. // propertys: an array of a property to find.
  262. //Output: object's property based on input array, or undefined if not found.
  263. function propertyValueAt(inputObject,propertys){
  264. //We need to find the property, which could be another object of properties.
  265. var propertyValue = inputObject;
  266. //Let's loop through propertys to go down the list.
  267. for(var i = 0; i < propertys.length; i++ ){
  268. propertyValue = propertyValue[propertys[i]];
  269. }
  270. return propertyValue;
  271. }
  272. /*
  273. var object = {
  274. a: 1,
  275. b: {
  276. c: 2,
  277. d: 3
  278. }
  279. };
  280.  
  281. console.log(propertyValueAt(object,['b','f']));
  282. */
  283. // --------------------------------------------------------------------------
  284. // Name: Christopher Cobb
  285. // --------------------------------------------------------------------------
  286.  
  287. // --------------------------------------------------------------------------
  288. // Exercise: Sum Nested Arrays
  289. // --------------------------------------------------------------------------
  290.  
  291. // Function sumNested
  292. // Input: addArray - an array of numbers and arrays of numbers to be added.
  293. // Return Output: The sum of all numbers (current code version has to have numbers)
  294.  
  295. //var maxLoop = 0; //Inf. loop protector cuz JSBin doesn't have a stop option.
  296. function sumNested(addArray){
  297. //Let's go for this approach:
  298. //for each element, we need to recurse on each element of the array./*
  299. /*maxLoop++;
  300. if( maxLoop > 1000 ){
  301. console.log("max hit");
  302. return 0;
  303. }*/
  304. var sum = 0;
  305. //So if we're just a number at a array element, add it up!
  306. // else dive into the array until we're at the elements then add them up.
  307.  
  308. for( var i = 0; i < addArray.length; i++){
  309. if(!Array.isArray(addArray[i])){
  310. console.log('Adding ' + addArray[i]);
  311. sum = sum + addArray[i]; // Single num found! Add it up!
  312. } else {
  313. console.log('recursive ' + addArray[i]);
  314. sum = sum + sumNested(addArray[i]); //Add up everything inside the array!
  315. }
  316. console.log("Sum:" + sum);
  317. }
  318. return sum;
  319. }
  320.  
  321. //console.log(sumNested([])); //23
  322.  
  323. // --------------------------------------------------------------------------
  324. // Name: Christopher Cobb
  325. // --------------------------------------------------------------------------
  326.  
  327. // --------------------------------------------------------------------------
  328. // Exercise: Word Count
  329. // --------------------------------------------------------------------------
  330.  
  331. //Function wordCount(sentence)
  332. // Input: A string of characters to count the # of words of.
  333. // Output: The number of words. Words are space seperated, and can consist
  334. // of any type of symbol that isn't a space or newline.
  335. function wordCount(sentence){
  336. //A simple problem, simply check for spaces in a small loop
  337. var numWords = 0;
  338. var atSpace = true;
  339. //Here's the logic. We start assuming we're at a space.
  340. //Check a letter, if we're at a space and the next letter is a real letter
  341. // we're at a new word!
  342. //If we're not at a space, then we end the current word at a space, then
  343. // check for the next word as above.
  344. for( var i = 0; i < sentence.length; i++){
  345. if(atSpace){
  346. if(sentence[i] !== ' ' && sentence[i] !== '\n'){
  347. atSpace = false;
  348. numWords++;
  349. }
  350. } else {
  351. atSpace = sentence[i] === ' ' || sentence[i] === '\n';
  352. }
  353. }
  354. return numWords;
  355. }
  356.  
  357. //console.log(wordCount('Its a newline\ntest! \n Hi!'));
  358. //Returns 5, "Its" "a" "newline" "test!" "Hi!"
  359.  
  360. // --------------------------------------------------------------------------
  361. // Name: Christopher Cobb
  362. // --------------------------------------------------------------------------
  363.  
  364. // --------------------------------------------------------------------------
  365. // Exercise: Anagram Tester
  366. // --------------------------------------------------------------------------
  367.  
  368. //function isNumberofEqual(testChar, stringA, stringB)
  369. //input: a character to test for the number of occurances
  370. // string 1 to check.
  371. // string 2 to check.
  372. //output: If the number of testCha occurances in the strings is the same
  373.  
  374. //function areTheseAnagrams(stringA,stringB)
  375. //input: stringa and stringb to check for anagrams.
  376. //output: if they are anagrams.
  377. function isNumberOfEqual(testChar, stringA ,stringB){
  378. var numA = 0;
  379. var numB = 0;
  380. //Let's count all the characters!
  381. for( var i = 0; i < stringA.length; i++){
  382. if(stringA.charAt(i) === testChar){
  383. numA++;
  384. }
  385. if(stringB.charAt(i) === testChar ){
  386. numB++;
  387. }
  388. }
  389. return numA === numB;
  390. }
  391.  
  392. function areTheseAnagrams(stringA,stringB){
  393. //Ok! We got two string! First, if they're not equal length they're not anas
  394. if( stringA.length != stringB.length)
  395. {
  396. return false;
  397. }
  398. //Now from here on they are equal length. My quick idea is a markoff method.
  399. //is to check if the character at i ocurs the same amount of times in both
  400. // strings, using isNumberofEqual.
  401. for(var i = 0; i < stringA.length; i++){
  402. //if(stringB.indexOf(stringA.charAt(i)) === -1){
  403. if( isNumberOfEqual(stringA.charAt(i),stringA,stringB) === false ){
  404. return false;
  405. }
  406. }
  407. return true;
  408. }
  409.  
  410.  
  411. //console.log(areTheseAnagrams("abc","cde"));
  412.  
  413. // --------------------------------------------------------------------------
  414. // Name: Christopher Cobb
  415. // --------------------------------------------------------------------------
  416.  
  417. // --------------------------------------------------------------------------
  418. // Exercise: Analyze Prices
  419. // --------------------------------------------------------------------------
  420.  
  421. //function analyzePrices(priceArray)
  422. //input: An array of prices, indexed by time.
  423. //output: an object of buyIndex: the index to buy the stock. sellIndex: the index to sell
  424. //Nulls if it's not profitable at all.
  425. function analyzePrices(priceArray){
  426. var priceIndex = new Object;
  427. priceIndex.buyIndex = 0;
  428. priceIndex.sellIndex = 0;
  429. var maxProfit = 0;
  430. var heldStock = 0;
  431. for( var i = 0; i < priceArray.length; i++){
  432. for( var j = 1; j < priceArray.length; j++){
  433. //find the profit for buying at i, selling at j
  434. if(i < j){ //can't sell before buy
  435. var profit = priceArray[j]-priceArray[i];
  436. if(profit > maxProfit){
  437. //record!
  438. priceIndex.buyIndex = i;
  439. priceIndex.sellIndex = j;
  440. maxProfit = profit;
  441. }
  442. }
  443. }
  444. }
  445. if( maxProfit === 0){
  446. priceIndex.buyIndex = null;
  447. priceIndex.sellIndex = null;
  448. }
  449. return priceIndex;
  450. }
  451. //console.log(analyzePrices([1,1,1,1,1]));
  452.  
  453. // --------------------------------------------------------------------------
  454. // Name: Christopher Cobb
  455. // --------------------------------------------------------------------------
  456.  
  457. // --------------------------------------------------------------------------
  458. // Exercise: Fizz Buzz
  459. // --------------------------------------------------------------------------
  460.  
  461. function fizzBuzz(n){
  462. var returnString = "";
  463. if( n <= 0 ){
  464. return returnString;
  465. }
  466. for( var i = 1; i <= n; i++){
  467. returnString += 1;
  468. if(i%3 === 0){
  469. returnString += 'fizz';
  470. }
  471.  
  472. if(i%5 === 0){
  473. returnString += 'buzz';
  474. }
  475. returnString += ","
  476.  
  477.  
  478.  
  479. }
  480.  
  481. return returnString;
  482. }
  483.  
  484.  
  485. //Really? Fizzbuzz?
  486. console.log(fizzBuzz(15));</script></body>
  487. </html>
Add Comment
Please, Sign In to add comment