Guest User

Untitled

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