Guest User

Untitled

a guest
Jan 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.50 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(inputObject, propertys)
  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(addArray)
  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. }*/ //uncomment to enable loop protection, and the var maxLoop above!
  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. if(!isNaN(addArray[i])){
  71.  
  72. sum = sum + addArray[i]; // Single num found! Add it up!
  73. }
  74.  
  75. } else {
  76. var addSum = sumNested(addArray[i]);
  77. if(!isNaN(addSum)){
  78. sum = sum + addSum;//sumNested(addArray[i]); //Add up everything inside the array!
  79. }
  80. }
  81. }
  82. return sum;
  83. }
  84.  
  85. //console.log(sumNested([1,1,1,['a',4,[8]],['a']])); //can ignore letters. 15.
  86.  
  87. // --------------------------------------------------------------------------
  88. // Name: Christopher Cobb
  89. // --------------------------------------------------------------------------
  90.  
  91. // --------------------------------------------------------------------------
  92. // Exercise: Word Count
  93. // --------------------------------------------------------------------------
  94.  
  95. //Function wordCount(sentence)
  96. // Input: A string of characters to count the # of words of.
  97. // Output: The number of words. Words are space seperated, and can consist
  98. // of any type of symbol that isn't a space or newline.
  99. function wordCount(sentence){
  100. //A simple problem, simply check for spaces in a small loop
  101. var numWords = 0;
  102. var atSpace = true;
  103. //Here's the logic. We start assuming we're at a space.
  104. //Check a letter, if we're at a space and the next letter is a real letter
  105. // we're at a new word!
  106. //If we're not at a space, then we end the current word at a space, then
  107. // check for the next word as above.
  108. for( var i = 0; i < sentence.length; i++){
  109. if(atSpace){
  110. if(sentence[i] !== ' ' && sentence[i] !== '\n'){
  111. atSpace = false;
  112. numWords++;
  113. }
  114. } else {
  115. atSpace = sentence[i] === ' ' || sentence[i] === '\n';
  116. }
  117. }
  118. return numWords;
  119. }
  120.  
  121. //console.log(wordCount('Its a newline\ntest! \n Hi!'));
  122. //Returns 5, "Its" "a" "newline" "test!" "Hi!"
  123.  
  124. // --------------------------------------------------------------------------
  125. // Name: Christopher Cobb
  126. // --------------------------------------------------------------------------
  127.  
  128. // --------------------------------------------------------------------------
  129. // Exercise: Anagram Tester
  130. // --------------------------------------------------------------------------
  131.  
  132. //function isNumberofEqual(testChar, stringA, stringB)
  133. //input: a character to test for the number of occurances
  134. // string 1 to check.
  135. // string 2 to check.
  136. //output: If the number of testCha occurances in the strings is the same
  137.  
  138. //function areTheseAnagrams(stringA,stringB)
  139. //input: stringa and stringb to check for anagrams.
  140. //output: if they are anagrams.
  141. function isNumberOfEqual(testChar, stringA ,stringB){
  142. var numA = 0;
  143. var numB = 0;
  144. //Let's count all the characters!
  145. for( var i = 0; i < stringA.length; i++){
  146. if(stringA.charAt(i) === testChar){
  147. numA++;
  148. }
  149. if(stringB.charAt(i) === testChar ){
  150. numB++;
  151. }
  152. }
  153. return numA === numB;
  154. }
  155.  
  156. function areTheseAnagrams(stringA,stringB){
  157. //Ok! We got two string! First, if they're not equal length they're not anas
  158. if( stringA.length != stringB.length)
  159. {
  160. return false;
  161. }
  162. //Now from here on they are equal length. My quick idea is a markoff method.
  163. //is to check if the character at i ocurs the same amount of times in both
  164. // strings, using isNumberofEqual.
  165. for(var i = 0; i < stringA.length; i++){
  166. //if(stringB.indexOf(stringA.charAt(i)) === -1){
  167. if( isNumberOfEqual(stringA.charAt(i),stringA,stringB) === false ){
  168. return false;
  169. }
  170. }
  171. return true;
  172. }
  173.  
  174.  
  175. //console.log(areTheseAnagrams("abc","cde"));
  176.  
  177. // --------------------------------------------------------------------------
  178. // Name: Christopher Cobb
  179. // --------------------------------------------------------------------------
  180.  
  181. // --------------------------------------------------------------------------
  182. // Exercise: Analyze Prices
  183. // --------------------------------------------------------------------------
  184.  
  185. //function analyzePrices(priceArray)
  186. //input: An array of prices, indexed by time.
  187. //output: an object of buyIndex: the index to buy the stock. sellIndex: the index to sell
  188. //Nulls if it's not profitable at all.
  189. function analyzePrices(priceArray){
  190. var priceIndex = new Object;
  191. priceIndex.buyIndex = 0;
  192. priceIndex.sellIndex = 0;
  193. var maxProfit = 0;
  194. var heldStock = 0;
  195. for( var i = 0; i < priceArray.length; i++){
  196. for( var j = 1; j < priceArray.length; j++){
  197. //find the profit for buying at i, selling at j
  198. if(i < j){ //can't sell before buy
  199. var profit = priceArray[j]-priceArray[i];
  200. if(profit > maxProfit){
  201. //record!
  202. priceIndex.buyIndex = i;
  203. priceIndex.sellIndex = j;
  204. maxProfit = profit;
  205. }
  206. }
  207. }
  208. }
  209. if( maxProfit === 0){
  210. priceIndex.buyIndex = null;
  211. priceIndex.sellIndex = null;
  212. }
  213. return priceIndex;
  214. }
  215. //console.log(analyzePrices([1,1,1,1,1]));
  216.  
  217. // --------------------------------------------------------------------------
  218. // Name: Christopher Cobb
  219. // --------------------------------------------------------------------------
  220.  
  221. // --------------------------------------------------------------------------
  222. // Exercise: Fizz Buzz
  223. // --------------------------------------------------------------------------
  224.  
  225. //function fizzBuzz(n)
  226. //input: A single number, n, to list out with 3's for fizes and 5 for buzzes.
  227. //output: a string of characters from 1 to n, with commas, and fizz's if divis.3 and
  228. //buzzes if div by 5. Both fizzbuzz if both.
  229.  
  230. function fizzBuzz(n){
  231. var returnString = "";
  232. if( n <= 0 ){
  233. return returnString;
  234. }
  235. for( var i = 1; i <= n; i++){
  236. returnString += i;
  237. if(i%3 === 0){
  238. returnString += 'fizz';
  239. }
  240.  
  241. if(i%5 === 0){
  242. returnString += 'buzz';
  243. }
  244. if( i != n){
  245. returnString += ","
  246. }
  247. }
  248.  
  249. return returnString;
  250. }
  251.  
  252.  
  253. //Really? Fizzbuzz?
  254. //console.log(fizzBuzz(15));
  255.  
  256. // --------------------------------------------------------------------------
  257. // Name: Christopher Cobb
  258. // --------------------------------------------------------------------------
  259.  
  260. // --------------------------------------------------------------------------
  261. // Exercise: Object Oriented Programming - Car
  262. // --------------------------------------------------------------------------
  263.  
  264. //function car() constructor function
  265. //has properties:
  266. //speed -> Speed of the car.
  267. //getSpeed -> function that returns the speed.
  268. //setSpeed -> function that sets the car speed. No going reverse so if < 0, car stops.
  269. //stop -> Sets speed to 0;
  270.  
  271. function Car(){
  272. this.speed = 0;
  273. this.getSpeed = function() {return this.speed;}
  274. this.setSpeed = function(newSpeed) {
  275. if(newSpeed > 0 ){
  276. this.speed = newSpeed;
  277. } else {
  278. this.speed = 0;
  279. }
  280. }
  281. this.stop = function() {this.speed = 0;}
  282. }
  283.  
  284. /*
  285. var car = new Car();
  286. console.log(car.getSpeed());
  287. car.setSpeed(10);
  288. console.log(car.getSpeed());
  289. car.stop();
  290. console.log(car.getSpeed());
  291. car.setSpeed(9);
  292. console.log(car.getSpeed());
  293. car.setSpeed(-1);
  294. console.log(car.getSpeed());
  295. car.setSpeed(8);
  296. console.log(car.getSpeed());
  297. car.setSpeed(null);
  298. console.log(car.getSpeed());
  299. car.stop();
  300. console.log(car.getSpeed());
  301. */
  302. // --------------------------------------------------------------------------
  303. // Name: Christopher Cobb
  304. // --------------------------------------------------------------------------
  305.  
  306. // --------------------------------------------------------------------------
  307. // Exercise: Bonus - Calculate Bowling Score
  308. // --------------------------------------------------------------------------
  309. function calculateBowlingScore(bowlingScore){
  310. var frameA = ' ';
  311. var frameB = ' ';
  312. var frameCount = 1;
  313. var bonus = [' ',' '];
  314. var score = 0;
  315. for(var i = 0; i < bowlingScore.length; i++){
  316. //Check for strikes
  317. if(bowlingScore.charAt(i) === 'X'){
  318. if(bonus[0] === 'X'){
  319. score = score + 10;
  320. bonus[0] = ' ';
  321. }
  322. if(bonus[1] === 'X'){
  323. score = score + 10;
  324. bonus[0] = 'X';
  325. }
  326. score = score + 10;
  327. if(bonus[0] === ' '){
  328. bonus[0] = 'X';
  329. }
  330. }// Lots of work to get this working, trying to save strikes for adding bonuses.
  331.  
  332. }
  333. }
  334.  
  335. //Yeah kinda doesn't work yet.
  336. </script>
  337.  
  338.  
  339.  
  340. <script id="jsbin-source-javascript" type="text/javascript">// --------------------------------------------------------------------------
  341. // Name: Christopher Cobb
  342. // --------------------------------------------------------------------------
  343.  
  344. // --------------------------------------------------------------------------
  345. // Exercise: Property Path Evaluation
  346. // --------------------------------------------------------------------------
  347.  
  348. //Function propertyValueAt(inputObject, propertys)
  349. //Input: object whose value to find.
  350. // propertys: an array of a property to find.
  351. //Output: object's property based on input array, or undefined if not found.
  352. function propertyValueAt(inputObject,propertys){
  353. //We need to find the property, which could be another object of properties.
  354. var propertyValue = inputObject;
  355. //Let's loop through propertys to go down the list.
  356. for(var i = 0; i < propertys.length; i++ ){
  357. propertyValue = propertyValue[propertys[i]];
  358. }
  359. return propertyValue;
  360. }
  361. /*
  362. var object = {
  363. a: 1,
  364. b: {
  365. c: 2,
  366. d: 3
  367. }
  368. };
  369.  
  370. console.log(propertyValueAt(object,['b','f']));
  371. */
  372. // --------------------------------------------------------------------------
  373. // Name: Christopher Cobb
  374. // --------------------------------------------------------------------------
  375.  
  376. // --------------------------------------------------------------------------
  377. // Exercise: Sum Nested Arrays
  378. // --------------------------------------------------------------------------
  379.  
  380. // Function sumNested(addArray)
  381. // Input: addArray - an array of numbers and arrays of numbers to be added.
  382. // Return Output: The sum of all numbers (current code version has to have numbers)
  383.  
  384. //var maxLoop = 0; //Inf. loop protector cuz JSBin doesn't have a stop option.
  385. function sumNested(addArray){
  386. //Let's go for this approach:
  387. //for each element, we need to recurse on each element of the array./*
  388. /*maxLoop++;
  389. if( maxLoop > 1000 ){
  390. console.log("max hit");
  391. return 0;
  392. }*/ //uncomment to enable loop protection, and the var maxLoop above!
  393. var sum = 0;
  394. //So if we're just a number at a array element, add it up!
  395. // else dive into the array until we're at the elements then add them up.
  396.  
  397. for( var i = 0; i < addArray.length; i++){
  398. if(!Array.isArray(addArray[i])){
  399. if(!isNaN(addArray[i])){
  400.  
  401. sum = sum + addArray[i]; // Single num found! Add it up!
  402. }
  403.  
  404. } else {
  405. var addSum = sumNested(addArray[i]);
  406. if(!isNaN(addSum)){
  407. sum = sum + addSum;//sumNested(addArray[i]); //Add up everything inside the array!
  408. }
  409. }
  410. }
  411. return sum;
  412. }
  413.  
  414. //console.log(sumNested([1,1,1,['a',4,[8]],['a']])); //can ignore letters. 15.
  415.  
  416. // --------------------------------------------------------------------------
  417. // Name: Christopher Cobb
  418. // --------------------------------------------------------------------------
  419.  
  420. // --------------------------------------------------------------------------
  421. // Exercise: Word Count
  422. // --------------------------------------------------------------------------
  423.  
  424. //Function wordCount(sentence)
  425. // Input: A string of characters to count the # of words of.
  426. // Output: The number of words. Words are space seperated, and can consist
  427. // of any type of symbol that isn't a space or newline.
  428. function wordCount(sentence){
  429. //A simple problem, simply check for spaces in a small loop
  430. var numWords = 0;
  431. var atSpace = true;
  432. //Here's the logic. We start assuming we're at a space.
  433. //Check a letter, if we're at a space and the next letter is a real letter
  434. // we're at a new word!
  435. //If we're not at a space, then we end the current word at a space, then
  436. // check for the next word as above.
  437. for( var i = 0; i < sentence.length; i++){
  438. if(atSpace){
  439. if(sentence[i] !== ' ' && sentence[i] !== '\n'){
  440. atSpace = false;
  441. numWords++;
  442. }
  443. } else {
  444. atSpace = sentence[i] === ' ' || sentence[i] === '\n';
  445. }
  446. }
  447. return numWords;
  448. }
  449.  
  450. //console.log(wordCount('Its a newline\ntest! \n Hi!'));
  451. //Returns 5, "Its" "a" "newline" "test!" "Hi!"
  452.  
  453. // --------------------------------------------------------------------------
  454. // Name: Christopher Cobb
  455. // --------------------------------------------------------------------------
  456.  
  457. // --------------------------------------------------------------------------
  458. // Exercise: Anagram Tester
  459. // --------------------------------------------------------------------------
  460.  
  461. //function isNumberofEqual(testChar, stringA, stringB)
  462. //input: a character to test for the number of occurances
  463. // string 1 to check.
  464. // string 2 to check.
  465. //output: If the number of testCha occurances in the strings is the same
  466.  
  467. //function areTheseAnagrams(stringA,stringB)
  468. //input: stringa and stringb to check for anagrams.
  469. //output: if they are anagrams.
  470. function isNumberOfEqual(testChar, stringA ,stringB){
  471. var numA = 0;
  472. var numB = 0;
  473. //Let's count all the characters!
  474. for( var i = 0; i < stringA.length; i++){
  475. if(stringA.charAt(i) === testChar){
  476. numA++;
  477. }
  478. if(stringB.charAt(i) === testChar ){
  479. numB++;
  480. }
  481. }
  482. return numA === numB;
  483. }
  484.  
  485. function areTheseAnagrams(stringA,stringB){
  486. //Ok! We got two string! First, if they're not equal length they're not anas
  487. if( stringA.length != stringB.length)
  488. {
  489. return false;
  490. }
  491. //Now from here on they are equal length. My quick idea is a markoff method.
  492. //is to check if the character at i ocurs the same amount of times in both
  493. // strings, using isNumberofEqual.
  494. for(var i = 0; i < stringA.length; i++){
  495. //if(stringB.indexOf(stringA.charAt(i)) === -1){
  496. if( isNumberOfEqual(stringA.charAt(i),stringA,stringB) === false ){
  497. return false;
  498. }
  499. }
  500. return true;
  501. }
  502.  
  503.  
  504. //console.log(areTheseAnagrams("abc","cde"));
  505.  
  506. // --------------------------------------------------------------------------
  507. // Name: Christopher Cobb
  508. // --------------------------------------------------------------------------
  509.  
  510. // --------------------------------------------------------------------------
  511. // Exercise: Analyze Prices
  512. // --------------------------------------------------------------------------
  513.  
  514. //function analyzePrices(priceArray)
  515. //input: An array of prices, indexed by time.
  516. //output: an object of buyIndex: the index to buy the stock. sellIndex: the index to sell
  517. //Nulls if it's not profitable at all.
  518. function analyzePrices(priceArray){
  519. var priceIndex = new Object;
  520. priceIndex.buyIndex = 0;
  521. priceIndex.sellIndex = 0;
  522. var maxProfit = 0;
  523. var heldStock = 0;
  524. for( var i = 0; i < priceArray.length; i++){
  525. for( var j = 1; j < priceArray.length; j++){
  526. //find the profit for buying at i, selling at j
  527. if(i < j){ //can't sell before buy
  528. var profit = priceArray[j]-priceArray[i];
  529. if(profit > maxProfit){
  530. //record!
  531. priceIndex.buyIndex = i;
  532. priceIndex.sellIndex = j;
  533. maxProfit = profit;
  534. }
  535. }
  536. }
  537. }
  538. if( maxProfit === 0){
  539. priceIndex.buyIndex = null;
  540. priceIndex.sellIndex = null;
  541. }
  542. return priceIndex;
  543. }
  544. //console.log(analyzePrices([1,1,1,1,1]));
  545.  
  546. // --------------------------------------------------------------------------
  547. // Name: Christopher Cobb
  548. // --------------------------------------------------------------------------
  549.  
  550. // --------------------------------------------------------------------------
  551. // Exercise: Fizz Buzz
  552. // --------------------------------------------------------------------------
  553.  
  554. //function fizzBuzz(n)
  555. //input: A single number, n, to list out with 3's for fizes and 5 for buzzes.
  556. //output: a string of characters from 1 to n, with commas, and fizz's if divis.3 and
  557. //buzzes if div by 5. Both fizzbuzz if both.
  558.  
  559. function fizzBuzz(n){
  560. var returnString = "";
  561. if( n <= 0 ){
  562. return returnString;
  563. }
  564. for( var i = 1; i <= n; i++){
  565. returnString += i;
  566. if(i%3 === 0){
  567. returnString += 'fizz';
  568. }
  569.  
  570. if(i%5 === 0){
  571. returnString += 'buzz';
  572. }
  573. if( i != n){
  574. returnString += ","
  575. }
  576. }
  577.  
  578. return returnString;
  579. }
  580.  
  581.  
  582. //Really? Fizzbuzz?
  583. //console.log(fizzBuzz(15));
  584.  
  585. // --------------------------------------------------------------------------
  586. // Name: Christopher Cobb
  587. // --------------------------------------------------------------------------
  588.  
  589. // --------------------------------------------------------------------------
  590. // Exercise: Object Oriented Programming - Car
  591. // --------------------------------------------------------------------------
  592.  
  593. //function car() constructor function
  594. //has properties:
  595. //speed -> Speed of the car.
  596. //getSpeed -> function that returns the speed.
  597. //setSpeed -> function that sets the car speed. No going reverse so if < 0, car stops.
  598. //stop -> Sets speed to 0;
  599.  
  600. function Car(){
  601. this.speed = 0;
  602. this.getSpeed = function() {return this.speed;}
  603. this.setSpeed = function(newSpeed) {
  604. if(newSpeed > 0 ){
  605. this.speed = newSpeed;
  606. } else {
  607. this.speed = 0;
  608. }
  609. }
  610. this.stop = function() {this.speed = 0;}
  611. }
  612.  
  613. /*
  614. var car = new Car();
  615. console.log(car.getSpeed());
  616. car.setSpeed(10);
  617. console.log(car.getSpeed());
  618. car.stop();
  619. console.log(car.getSpeed());
  620. car.setSpeed(9);
  621. console.log(car.getSpeed());
  622. car.setSpeed(-1);
  623. console.log(car.getSpeed());
  624. car.setSpeed(8);
  625. console.log(car.getSpeed());
  626. car.setSpeed(null);
  627. console.log(car.getSpeed());
  628. car.stop();
  629. console.log(car.getSpeed());
  630. */
  631. // --------------------------------------------------------------------------
  632. // Name: Christopher Cobb
  633. // --------------------------------------------------------------------------
  634.  
  635. // --------------------------------------------------------------------------
  636. // Exercise: Bonus - Calculate Bowling Score
  637. // --------------------------------------------------------------------------
  638. function calculateBowlingScore(bowlingScore){
  639. var frameA = ' ';
  640. var frameB = ' ';
  641. var frameCount = 1;
  642. var bonus = [' ',' '];
  643. var score = 0;
  644. for(var i = 0; i < bowlingScore.length; i++){
  645. //Check for strikes
  646. if(bowlingScore.charAt(i) === 'X'){
  647. if(bonus[0] === 'X'){
  648. score = score + 10;
  649. bonus[0] = ' ';
  650. }
  651. if(bonus[1] === 'X'){
  652. score = score + 10;
  653. bonus[0] = 'X';
  654. }
  655. score = score + 10;
  656. if(bonus[0] === ' '){
  657. bonus[0] = 'X';
  658. }
  659. }// Lots of work to get this working, trying to save strikes for adding bonuses.
  660.  
  661. }
  662. }
  663.  
  664. //Yeah kinda doesn't work yet.</script></body>
  665. </html>
Add Comment
Please, Sign In to add comment