Guest User

Untitled

a guest
Nov 16th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.81 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: Zach Burmaster
  13. //-------------------------------------------------
  14.  
  15.  
  16. //-------------------------------------------------
  17. //Exercise: Property Path Evaluation
  18. //-------------------------------------------------
  19.  
  20.  
  21. function propertyValueAt(object, array) {
  22. //didn't get to this, need to figure out a way to spread the number of things
  23. //in the array over this below, could do this by looping through and using the same
  24. // [array[index]] notation on the same thing over and over again
  25. return object[array[0]][array[1]];
  26.  
  27. }
  28.  
  29. var object = {a: 1, b: {c: 2, d: 3}};
  30.  
  31. console.log(propertyValueAt(object, ['b', 'c']));
  32.  
  33.  
  34. //-------------------------------------------------
  35. //Exercise: Sum Nested Arrays
  36. //-------------------------------------------------
  37.  
  38. function sumNested(arr) {
  39. //1 didn't get to checking the number of nested arrays, need to do that using the pseudocode below:
  40. //2 looping over the length of the array
  41. //3 take the first thing in the array {
  42. //4 if it's a number, add it to the newNumber variable {
  43. //5 else, do what's on line 2 again with this array
  44.  
  45.  
  46. let newArr = arr
  47. .flat(2)
  48. .reduce((accumulator, currentValue) => accumulator + currentValue)
  49.  
  50. return newArr;
  51. }
  52.  
  53. let testArr = [1, 1, 1, [3, 4, [8]], [5]];
  54. console.log(sumNested(testArr));;
  55.  
  56.  
  57. //-------------------------------------------------
  58. //Exercise: Word Count
  59. //-------------------------------------------------
  60.  
  61. function wordCount(sentence) {
  62. var counter = 0;
  63. for (var i=0; i<=sentence.length; i++) {
  64. //feels close here, fails on string of spaces though
  65. if (sentence[i] == " " && (sentence[i+1] != " " || i == sentence.length)) {
  66. counter += 1
  67. }
  68. }
  69. return counter+1;
  70. }
  71.  
  72.  
  73.  
  74. console.log(wordCount("Thisiashortsentence!"))
  75.  
  76.  
  77. //-------------------------------------------------
  78. //Exercise: Anagram Tester
  79. //-------------------------------------------------
  80.  
  81. function areTheseAnagrams(str1, str2) {
  82. let arr1 = []
  83. for (i=0; i<str1.length; i++) {
  84. arr1.push(str1[i]);
  85. }
  86.  
  87. let arr2= [];
  88. for (i=0; i<str2.length; i++) {
  89. arr2.push(str2[i]);
  90. }
  91.  
  92. let str1Sorted = arr1.sort().join()
  93. let str2Sorted = arr2.sort().join()
  94.  
  95.  
  96.  
  97. if (str1Sorted == str2Sorted) {
  98. return true;
  99. } else {
  100. return false;
  101. }
  102.  
  103.  
  104. }
  105.  
  106. console.log(areTheseAnagrams("dingdong", "dongding"));
  107.  
  108.  
  109. //-------------------------------------------------
  110. //Exercise: Analyze Prices
  111. //-------------------------------------------------
  112.  
  113. //didn't get to this one
  114.  
  115.  
  116.  
  117. //------------------------------------------------
  118. //Exercise: Fizz Buzz
  119. //------------------------------------------------
  120.  
  121.  
  122. function fizzBuzz(n) {
  123. let answerString = ""
  124. for (i=0; i<=n; i++) {
  125. if (i%15 === 0 && i!=0) {
  126. answerString += i;
  127. answerString+='fizzbuzz, ';
  128. } else if (i%5 === 0 & i!=0) {
  129. answerString += i;
  130. answerString += 'buzz, ';
  131. } else if (i%3 === 0 & i!= 0) {
  132. answerString += i;
  133. answerString += 'fizz, ';
  134. } else {
  135. if (i !== 0) {
  136. answerString+= i;
  137. answerString+= ', ';
  138. }
  139. }
  140.  
  141. }
  142. // needed to add the case on each if where i is the last number then don't put a comma in,
  143. // felt like my time is better spent elsewhere
  144. return answerString;
  145. }
  146.  
  147.  
  148. console.log(fizzBuzz(15))
  149.  
  150. //------------------------------------------------
  151. //Exercise: Object Oriented Programming - Car
  152. //------------------------------------------------
  153.  
  154.  
  155. function Car() {
  156. this.speed = 0;
  157. this.getSpeed = function () {return this.speed;}
  158.  
  159.  
  160. this.setSpeed = function (speed) {
  161. if (speed >= 0) {
  162. this.speed = speed;}
  163. };
  164.  
  165. this.stop = function () {
  166. this.speed = 0;
  167. }
  168.  
  169. }
  170.  
  171. var myCar = new Car();
  172. console.log(myCar.getSpeed());
  173. myCar.setSpeed(10);
  174. console.log(myCar.getSpeed());
  175. myCar.stop();
  176. console.log(myCar.getSpeed());
  177.  
  178.  
  179.  
  180. </script>
  181.  
  182.  
  183.  
  184. <script id="jsbin-source-javascript" type="text/javascript">//-------------------------------------------------
  185. //Name: Zach Burmaster
  186. //-------------------------------------------------
  187.  
  188.  
  189. //-------------------------------------------------
  190. //Exercise: Property Path Evaluation
  191. //-------------------------------------------------
  192.  
  193.  
  194. function propertyValueAt(object, array) {
  195. //didn't get to this, need to figure out a way to spread the number of things
  196. //in the array over this below, could do this by looping through and using the same
  197. // [array[index]] notation on the same thing over and over again
  198. return object[array[0]][array[1]];
  199.  
  200. }
  201.  
  202. var object = {a: 1, b: {c: 2, d: 3}};
  203.  
  204. console.log(propertyValueAt(object, ['b', 'c']));
  205.  
  206.  
  207. //-------------------------------------------------
  208. //Exercise: Sum Nested Arrays
  209. //-------------------------------------------------
  210.  
  211. function sumNested(arr) {
  212. //1 didn't get to checking the number of nested arrays, need to do that using the pseudocode below:
  213. //2 looping over the length of the array
  214. //3 take the first thing in the array {
  215. //4 if it's a number, add it to the newNumber variable {
  216. //5 else, do what's on line 2 again with this array
  217.  
  218.  
  219. let newArr = arr
  220. .flat(2)
  221. .reduce((accumulator, currentValue) => accumulator + currentValue)
  222.  
  223. return newArr;
  224. }
  225.  
  226. let testArr = [1, 1, 1, [3, 4, [8]], [5]];
  227. console.log(sumNested(testArr));;
  228.  
  229.  
  230. //-------------------------------------------------
  231. //Exercise: Word Count
  232. //-------------------------------------------------
  233.  
  234. function wordCount(sentence) {
  235. var counter = 0;
  236. for (var i=0; i<=sentence.length; i++) {
  237. //feels close here, fails on string of spaces though
  238. if (sentence[i] == " " && (sentence[i+1] != " " || i == sentence.length)) {
  239. counter += 1
  240. }
  241. }
  242. return counter+1;
  243. }
  244.  
  245.  
  246.  
  247. console.log(wordCount("Thisiashortsentence!"))
  248.  
  249.  
  250. //-------------------------------------------------
  251. //Exercise: Anagram Tester
  252. //-------------------------------------------------
  253.  
  254. function areTheseAnagrams(str1, str2) {
  255. let arr1 = []
  256. for (i=0; i<str1.length; i++) {
  257. arr1.push(str1[i]);
  258. }
  259.  
  260. let arr2= [];
  261. for (i=0; i<str2.length; i++) {
  262. arr2.push(str2[i]);
  263. }
  264.  
  265. let str1Sorted = arr1.sort().join()
  266. let str2Sorted = arr2.sort().join()
  267.  
  268.  
  269.  
  270. if (str1Sorted == str2Sorted) {
  271. return true;
  272. } else {
  273. return false;
  274. }
  275.  
  276.  
  277. }
  278.  
  279. console.log(areTheseAnagrams("dingdong", "dongding"));
  280.  
  281.  
  282. //-------------------------------------------------
  283. //Exercise: Analyze Prices
  284. //-------------------------------------------------
  285.  
  286. //didn't get to this one
  287.  
  288.  
  289.  
  290. //------------------------------------------------
  291. //Exercise: Fizz Buzz
  292. //------------------------------------------------
  293.  
  294.  
  295. function fizzBuzz(n) {
  296. let answerString = ""
  297. for (i=0; i<=n; i++) {
  298. if (i%15 === 0 && i!=0) {
  299. answerString += i;
  300. answerString+='fizzbuzz, ';
  301. } else if (i%5 === 0 & i!=0) {
  302. answerString += i;
  303. answerString += 'buzz, ';
  304. } else if (i%3 === 0 & i!= 0) {
  305. answerString += i;
  306. answerString += 'fizz, ';
  307. } else {
  308. if (i !== 0) {
  309. answerString+= i;
  310. answerString+= ', ';
  311. }
  312. }
  313.  
  314. }
  315. // needed to add the case on each if where i is the last number then don't put a comma in,
  316. // felt like my time is better spent elsewhere
  317. return answerString;
  318. }
  319.  
  320.  
  321. console.log(fizzBuzz(15))
  322.  
  323. //------------------------------------------------
  324. //Exercise: Object Oriented Programming - Car
  325. //------------------------------------------------
  326.  
  327.  
  328. function Car() {
  329. this.speed = 0;
  330. this.getSpeed = function () {return this.speed;}
  331.  
  332.  
  333. this.setSpeed = function (speed) {
  334. if (speed >= 0) {
  335. this.speed = speed;}
  336. };
  337.  
  338. this.stop = function () {
  339. this.speed = 0;
  340. }
  341.  
  342. }
  343.  
  344. var myCar = new Car();
  345. console.log(myCar.getSpeed());
  346. myCar.setSpeed(10);
  347. console.log(myCar.getSpeed());
  348. myCar.stop();
  349. console.log(myCar.getSpeed());
  350.  
  351.  
  352. </script></body>
  353. </html>
Add Comment
Please, Sign In to add comment