• API
• FAQ
• Tools
• Archive
daily pastebin goal
16%
SHARE
TWEET

# Untitled

a guest Jan 21st, 2018 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. <!DOCTYPE html>
2. <html>
4.   <meta charset="utf-8">
5.   <meta name="viewport" content="width=device-width">
6.   <title>JS Bin</title>
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']));
42.
43.
44. // --------------------------------------------------------------------------
45. // Name: Christopher Cobb
46. // --------------------------------------------------------------------------
47.
48. // --------------------------------------------------------------------------
49. // Exercise: Sum Nested Arrays
50. // --------------------------------------------------------------------------
51.
53. // Input: addArray - an array of numbers and arrays of numbers to be added.
54. // Return Output: The sum of all numbers (current code version has to have numbers)
55.
56. //var maxLoop = 0; //Inf. loop protector cuz JSBin doesn't have a stop option.
58.   //Let's go for this approach:
59.   //for each element, we need to recurse on each element of the array./*
60.   /*maxLoop++;
61.   if( maxLoop > 1000 ){
62.     console.log("max hit");
63.     return 0;
64.   }*/ //uncomment to enable loop protection, and the var maxLoop above!
65.   var sum = 0;
66.   //So if we're just a number at a array element, add it up!
67.   // else dive into the array until we're at the elements then add them up.
68.
69.   for( var i = 0; i < addArray.length; i++){
72.         sum = sum + addArray[i]; // Single num found! Add it up!
73.       }
74.
75.     } else {
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;
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!
203.           priceIndex.sellIndex = j;
204.           maxProfit = profit;
205.         }
206.       }
207.     }
208.   }
209.   if( maxProfit === 0){
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. //Hi!!!!
335. //Yeah kinda doesn't work yet
336.
337. </script>
338.
339.
340.
341. <script id="jsbin-source-javascript" type="text/javascript">// --------------------------------------------------------------------------
342. // Name: Christopher Cobb
343. // --------------------------------------------------------------------------
344.
345. // --------------------------------------------------------------------------
346. // Exercise: Property Path Evaluation
347. // --------------------------------------------------------------------------
348.
349. //Function propertyValueAt(inputObject, propertys)
350. //Input: object whose value to find.
351. //       propertys: an array of a property to find.
352. //Output: object's property based on input array, or undefined if not found.
353. function propertyValueAt(inputObject,propertys){
354.   //We need to find the property, which could be another object of properties.
355.   var propertyValue = inputObject;
356.   //Let's loop through propertys to go down the list.
357.   for(var i = 0; i < propertys.length; i++ ){
358.     propertyValue = propertyValue[propertys[i]];
359.   }
360.   return propertyValue;
361. }
362. /*
363. var object = {
364.   a: 1,
365.   b: {
366.     c: 2,
367.     d: 3
368.   }
369. };
370. */
371. //console.log(propertyValueAt(object,['b']));
372.
373.
374. // --------------------------------------------------------------------------
375. // Name: Christopher Cobb
376. // --------------------------------------------------------------------------
377.
378. // --------------------------------------------------------------------------
379. // Exercise: Sum Nested Arrays
380. // --------------------------------------------------------------------------
381.
383. // Input: addArray - an array of numbers and arrays of numbers to be added.
384. // Return Output: The sum of all numbers (current code version has to have numbers)
385.
386. //var maxLoop = 0; //Inf. loop protector cuz JSBin doesn't have a stop option.
388.   //Let's go for this approach:
389.   //for each element, we need to recurse on each element of the array./*
390.   /*maxLoop++;
391.   if( maxLoop > 1000 ){
392.     console.log("max hit");
393.     return 0;
394.   }*/ //uncomment to enable loop protection, and the var maxLoop above!
395.   var sum = 0;
396.   //So if we're just a number at a array element, add it up!
397.   // else dive into the array until we're at the elements then add them up.
398.
399.   for( var i = 0; i < addArray.length; i++){
402.         sum = sum + addArray[i]; // Single num found! Add it up!
403.       }
404.
405.     } else {
409.       }
410.     }
411.   }
412.   return sum;
413. }
414.
415. //console.log(sumNested([1,1,1,['a',4,[8]],['a']])); //can ignore letters. 15.
416.
417. // --------------------------------------------------------------------------
418. // Name: Christopher Cobb
419. // --------------------------------------------------------------------------
420.
421. // --------------------------------------------------------------------------
422. // Exercise: Word Count
423. // --------------------------------------------------------------------------
424.
425. //Function wordCount(sentence)
426. // Input: A string of characters to count the # of words of.
427. // Output: The number of words.  Words are space seperated, and can consist
428. //         of any type of symbol that isn't a space or newline.
429. function wordCount(sentence){
430.   //A simple problem, simply check for spaces in a small loop
431.   var numWords = 0;
432.   var atSpace = true;
433.   //Here's the logic.  We start assuming we're at a space.
434.   //Check a letter, if we're at a space and the next letter is a real letter
435.   //  we're at a new word!
436.   //If we're not at a space, then we end the current word at a space, then
437.   // check for the next word as above.
438.   for( var i = 0; i < sentence.length; i++){
439.     if(atSpace){
440.       if(sentence[i] !== ' ' && sentence[i] !== '\n'){
441.         atSpace = false;
442.         numWords++;
443.       }
444.     } else {
445.       atSpace = sentence[i] === ' ' || sentence[i] === '\n';
446.     }
447.   }
448.   return numWords;
449. }
450.
451. //console.log(wordCount('Its a newline\ntest! \n Hi!'));
452. //Returns 5, "Its" "a" "newline" "test!" "Hi!"
453.
454. // --------------------------------------------------------------------------
455. // Name: Christopher Cobb
456. // --------------------------------------------------------------------------
457.
458. // --------------------------------------------------------------------------
459. // Exercise: Anagram Tester
460. // --------------------------------------------------------------------------
461.
462. //function isNumberofEqual(testChar, stringA, stringB)
463. //input: a character to test for the number of occurances
464. //       string 1 to check.
465. //       string 2 to check.
466. //output: If the number of testCha occurances in the strings is the same
467.
468. //function areTheseAnagrams(stringA,stringB)
469. //input: stringa and stringb to check for anagrams.
470. //output: if they are anagrams.
471. function isNumberOfEqual(testChar, stringA ,stringB){
472.   var numA = 0;
473.   var numB = 0;
474.   //Let's count all the characters!
475.   for( var i = 0; i < stringA.length; i++){
476.     if(stringA.charAt(i) === testChar){
477.       numA++;
478.     }
479.     if(stringB.charAt(i) === testChar ){
480.       numB++;
481.     }
482.   }
483.   return numA === numB;
484. }
485.
486. function areTheseAnagrams(stringA,stringB){
487.   //Ok!  We got two string!  First, if they're not equal length they're not anas
488.   if( stringA.length != stringB.length)
489.     {
490.       return false;
491.     }
492.   //Now from here on they are equal length.  My quick idea is a markoff method.
493.   //is to check if the character at i ocurs the same amount of times in both
494.   // strings, using isNumberofEqual.
495.   for(var i = 0; i < stringA.length; i++){
496.     //if(stringB.indexOf(stringA.charAt(i)) === -1){
497.     if( isNumberOfEqual(stringA.charAt(i),stringA,stringB) === false ){
498.       return false;
499.     }
500.   }
501.   return true;
502. }
503.
504.
505. //console.log(areTheseAnagrams("abc","cde"));
506.
507. // --------------------------------------------------------------------------
508. // Name: Christopher Cobb
509. // --------------------------------------------------------------------------
510.
511. // --------------------------------------------------------------------------
512. // Exercise: Analyze Prices
513. // --------------------------------------------------------------------------
514.
515. //function analyzePrices(priceArray)
516. //input: An array of prices, indexed by time.
517. //output: an object of buyIndex: the index to buy the stock. sellIndex: the index to sell
518. //Nulls if it's not profitable at all.
519. function analyzePrices(priceArray){
520.   var priceIndex = new Object;
522.   priceIndex.sellIndex = 0;
523.   var maxProfit = 0;
524.   var heldStock = 0;
525.   for( var i = 0; i < priceArray.length; i++){
526.     for( var j = 1; j < priceArray.length; j++){
527.       //find the profit for buying at i, selling at j
528.       if(i < j){ //can't sell before buy
529.         var profit = priceArray[j]-priceArray[i];
530.         if(profit > maxProfit){
531.           //record!
533.           priceIndex.sellIndex = j;
534.           maxProfit = profit;
535.         }
536.       }
537.     }
538.   }
539.   if( maxProfit === 0){
541.     priceIndex.sellIndex = null;
542.   }
543.   return priceIndex;
544. }
545. //console.log(analyzePrices([1,1,1,1,1]));
546.
547. // --------------------------------------------------------------------------
548. // Name: Christopher Cobb
549. // --------------------------------------------------------------------------
550.
551. // --------------------------------------------------------------------------
552. // Exercise: Fizz Buzz
553. // --------------------------------------------------------------------------
554.
555. //function fizzBuzz(n)
556. //input: A single number, n, to list out with 3's for fizes and 5 for buzzes.
557. //output: a string of characters from 1 to n, with commas, and fizz's if divis.3 and
558. //buzzes if div by 5.  Both fizzbuzz if both.
559.
560. function fizzBuzz(n){
561.   var returnString = "";
562.   if( n <= 0 ){
563.     return returnString;
564.   }
565.   for( var i = 1; i <= n; i++){
566.     returnString += i;
567.     if(i%3 === 0){
568.       returnString += 'fizz';
569.     }
570.
571.     if(i%5 === 0){
572.       returnString += 'buzz';
573.     }
574.     if( i != n){
575.       returnString += ","
576.     }
577.   }
578.
579.   return returnString;
580. }
581.
582.
583. //Really?  Fizzbuzz?
584. //console.log(fizzBuzz(15));
585.
586. // --------------------------------------------------------------------------
587. // Name: Christopher Cobb
588. // --------------------------------------------------------------------------
589.
590. // --------------------------------------------------------------------------
591. // Exercise: Object Oriented Programming - Car
592. // --------------------------------------------------------------------------
593.
594. //function car() constructor function
595. //has properties:
596. //speed  -> Speed of the car.
597. //getSpeed -> function that returns the speed.
598. //setSpeed -> function that sets the car speed.  No going reverse so if < 0, car stops.
599. //stop -> Sets speed to 0;
600.
601. function Car(){
602.   this.speed = 0;
603.   this.getSpeed = function() {return this.speed;}
604.   this.setSpeed = function(newSpeed) {
605.     if(newSpeed > 0 ){
606.       this.speed = newSpeed;
607.     } else {
608.       this.speed = 0;
609.     }
610.   }
611.   this.stop = function() {this.speed = 0;}
612. }
613.
614. /*
615. var car = new Car();
616. console.log(car.getSpeed());
617. car.setSpeed(10);
618. console.log(car.getSpeed());
619. car.stop();
620. console.log(car.getSpeed());
621. car.setSpeed(9);
622. console.log(car.getSpeed());
623. car.setSpeed(-1);
624. console.log(car.getSpeed());
625. car.setSpeed(8);
626. console.log(car.getSpeed());
627. car.setSpeed(null);
628. console.log(car.getSpeed());
629. car.stop();
630. console.log(car.getSpeed());
631. */
632.   // --------------------------------------------------------------------------
633. // Name: Christopher Cobb
634. // --------------------------------------------------------------------------
635.
636. // --------------------------------------------------------------------------
637. // Exercise: Bonus - Calculate Bowling Score
638. // --------------------------------------------------------------------------
639. function calculateBowlingScore(bowlingScore){
640.   var frameA = ' ';
641.   var frameB = ' ';
642.   var frameCount = 1;
643.   var bonus = [' ',' '];
644.   var score = 0;
645.   for(var i = 0; i < bowlingScore.length; i++){
646.     //Check for strikes
647.     if(bowlingScore.charAt(i) === 'X'){
648.       if(bonus[0] === 'X'){
649.         score = score + 10;
650.         bonus[0] = ' ';
651.       }
652.       if(bonus[1] === 'X'){
653.         score = score + 10;
654.         bonus[0] = 'X';
655.       }
656.       score = score + 10;
657.       if(bonus[0] === ' '){
658.         bonus[0] = 'X';
659.       }
660.     }// Lots of work to get this working, trying to save strikes for adding bonuses.
661.
662.   }
663. }
664. //Hi!!!!
665. //Yeah kinda doesn't work yet
666. </script></body>
667. </html>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top