Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.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. //Paige Williams
  13. //----------------------------------------------------
  14.  
  15. //----------------------------------------------------
  16. //Exercise: Property Path Evaluation
  17. //----------------------------------------------------
  18. /*
  19. //loop through array and print values
  20. function propertyValueAt(object, arr) {
  21. return Object.values(object)
  22. }
  23. var object = {
  24. a: 1,
  25. b: {
  26. c: 2,
  27. d: 3
  28. }
  29. };
  30.  
  31. console.log(propertyValueAt(object, ['a']));
  32. console.log(propertyValueAt(object, ['b']));
  33. console.log(propertyValueAt(object, ['c']));
  34. console.log(propertyValueAt(object, ['d']));
  35. console.log(propertyValueAt(object, ['z']));
  36. */
  37.  
  38.  
  39. //----------------------------------------------------
  40. //Exercise: Sum Nested Arrays
  41. //----------------------------------------------------
  42. /*
  43. //loops through array and any arrays within the array
  44. // adding each number as it goes
  45. function sumNested(arr) {
  46. var sum = 0;
  47. for (var i = 0; i < arr.length; i++) {
  48. for (var j = 0; j < arr[i].length; j++) {
  49. sum += arr[i][j]
  50. }
  51. }
  52. return sum;
  53. }
  54. console.log(sumNested([1, 1, 1, [3, 4, [8]], [5]]));
  55. console.log(sumNested([]))
  56. */
  57.  
  58. //----------------------------------------------------
  59. //Exercise: Word Count
  60. //----------------------------------------------------
  61. /*
  62. //loops through the string checking for spaces, counts spaces in between words and
  63. // returns the final count + 1 to add in the first word
  64. function wordCount(sentence) {
  65. var count = 0;
  66. for (var i = 0; i < sentence.length; i++) {
  67. if (sentence[i] === ' ') {
  68. count++;
  69. }
  70. }
  71. return count + 1;
  72. }
  73.  
  74. console.log(wordCount('This is a short sentence!'));
  75. console.log(wordCount('ThisIsA!$LongWord!'));
  76. console.log(wordCount(' ')); //doesn't return correctly due to counting of the spaces
  77. */
  78.  
  79. //----------------------------------------------------
  80. //Exercise: Anagram Tester
  81. //----------------------------------------------------
  82. /*
  83. //sorts the strings in alaphebetical order, then checks if they are equal
  84. function areTheseAnagrams(str1, str2) {
  85. var arr1 = str1.split('');
  86. var sorted1 = arr1.sort();
  87. var arr2 = str2.split('');
  88. var sorted2 = arr2.sort();
  89. if (sorted1.join('') == sorted2.join('')) {
  90. return true;
  91. } else {
  92. return false;
  93. }
  94. }
  95. (console.log(areTheseAnagrams('abc', 'bac')));
  96. (console.log(areTheseAnagrams('abc', 'cde')));
  97. */
  98.  
  99. //----------------------------------------------------
  100. //Exercise: Analyze Prices
  101. //----------------------------------------------------
  102. /*
  103. function analyzePrices(prices) {
  104. var test;
  105. var maxProfit = -500;
  106. var buyIndex;
  107. var sellIndex;
  108. for (var i = 0; i < prices.length; i++) {
  109. for (var j = 0; j < prices.length; j++) {
  110. if (i < j) {
  111. test = prices[j] - prices[i]
  112. if (test > maxProfit) {
  113. maxProfit = test;
  114. buyIndex = i;
  115. sellIndex = j;
  116. }
  117. }
  118. }
  119. }
  120. console.log('Max Profit ' + maxProfit + ' ' +
  121. '{ buy index: ' + buyIndex + ', ' + sellIndex + '}');
  122. }
  123. analyzePrices([1,2,3,4,5,6,7,8,9,0]);
  124. analyzePrices([8,4,3,1,5,5,2,8,2,3]);
  125. */
  126. //----------------------------------------------------
  127. //Exercise: Fizz Buzz
  128. //----------------------------------------------------
  129. /*
  130. //prints numbers from 1 to n, checks if the remainder of i / 3, i / 5, = 0,
  131. // if so, prints fizz and buzz accordingly
  132. function fizzBuzz(n) {
  133. if (n <= 0) {
  134. return '';
  135. }
  136. for (var i = 1; i < n; i++) {
  137. if ((i % 3 === 0) && (i & 5 === 0)) {
  138. console.log(i + 'fizzbuzz');
  139. } else if (i % 3 === 0) {
  140. console.log(i + 'fizz');
  141. } else if (i % 5 === 0) {
  142. console.log(i + 'buzz');
  143. } else {
  144. console.log(i);
  145. }
  146. }
  147. }
  148.  
  149. console.log(fizzBuzz(100));
  150. console.log(fizzBuzz(-5));
  151. */
  152.  
  153. //----------------------------------------------------
  154. //Exercise: Car
  155. //----------------------------------------------------
  156. /*
  157. class Car {
  158. constructor() {
  159. this._speed = 0;
  160. }
  161.  
  162. getSpeed() {
  163. return this._speed;
  164. }
  165.  
  166. setSpeed(speed) {
  167. this._speed = speed;
  168. }
  169.  
  170. stop() {
  171. this._speed = 0;
  172. }
  173. }
  174. var car = new Car();
  175. console.log(car.getSpeed());
  176. car.setSpeed(10);
  177. console.log(car.getSpeed());
  178. car.stop();
  179. console.log(car.getSpeed());
  180. */
  181. </script>
  182.  
  183.  
  184.  
  185. <script id="jsbin-source-javascript" type="text/javascript">//----------------------------------------------------
  186. //Paige Williams
  187. //----------------------------------------------------
  188.  
  189. //----------------------------------------------------
  190. //Exercise: Property Path Evaluation
  191. //----------------------------------------------------
  192. /*
  193. //loop through array and print values
  194. function propertyValueAt(object, arr) {
  195. return Object.values(object)
  196. }
  197. var object = {
  198. a: 1,
  199. b: {
  200. c: 2,
  201. d: 3
  202. }
  203. };
  204.  
  205. console.log(propertyValueAt(object, ['a']));
  206. console.log(propertyValueAt(object, ['b']));
  207. console.log(propertyValueAt(object, ['c']));
  208. console.log(propertyValueAt(object, ['d']));
  209. console.log(propertyValueAt(object, ['z']));
  210. */
  211.  
  212.  
  213. //----------------------------------------------------
  214. //Exercise: Sum Nested Arrays
  215. //----------------------------------------------------
  216. /*
  217. //loops through array and any arrays within the array
  218. // adding each number as it goes
  219. function sumNested(arr) {
  220. var sum = 0;
  221. for (var i = 0; i < arr.length; i++) {
  222. for (var j = 0; j < arr[i].length; j++) {
  223. sum += arr[i][j]
  224. }
  225. }
  226. return sum;
  227. }
  228. console.log(sumNested([1, 1, 1, [3, 4, [8]], [5]]));
  229. console.log(sumNested([]))
  230. */
  231.  
  232. //----------------------------------------------------
  233. //Exercise: Word Count
  234. //----------------------------------------------------
  235. /*
  236. //loops through the string checking for spaces, counts spaces in between words and
  237. // returns the final count + 1 to add in the first word
  238. function wordCount(sentence) {
  239. var count = 0;
  240. for (var i = 0; i < sentence.length; i++) {
  241. if (sentence[i] === ' ') {
  242. count++;
  243. }
  244. }
  245. return count + 1;
  246. }
  247.  
  248. console.log(wordCount('This is a short sentence!'));
  249. console.log(wordCount('ThisIsA!$LongWord!'));
  250. console.log(wordCount(' ')); //doesn't return correctly due to counting of the spaces
  251. */
  252.  
  253. //----------------------------------------------------
  254. //Exercise: Anagram Tester
  255. //----------------------------------------------------
  256. /*
  257. //sorts the strings in alaphebetical order, then checks if they are equal
  258. function areTheseAnagrams(str1, str2) {
  259. var arr1 = str1.split('');
  260. var sorted1 = arr1.sort();
  261. var arr2 = str2.split('');
  262. var sorted2 = arr2.sort();
  263. if (sorted1.join('') == sorted2.join('')) {
  264. return true;
  265. } else {
  266. return false;
  267. }
  268. }
  269. (console.log(areTheseAnagrams('abc', 'bac')));
  270. (console.log(areTheseAnagrams('abc', 'cde')));
  271. */
  272.  
  273. //----------------------------------------------------
  274. //Exercise: Analyze Prices
  275. //----------------------------------------------------
  276. /*
  277. function analyzePrices(prices) {
  278. var test;
  279. var maxProfit = -500;
  280. var buyIndex;
  281. var sellIndex;
  282. for (var i = 0; i < prices.length; i++) {
  283. for (var j = 0; j < prices.length; j++) {
  284. if (i < j) {
  285. test = prices[j] - prices[i]
  286. if (test > maxProfit) {
  287. maxProfit = test;
  288. buyIndex = i;
  289. sellIndex = j;
  290. }
  291. }
  292. }
  293. }
  294. console.log('Max Profit ' + maxProfit + ' ' +
  295. '{ buy index: ' + buyIndex + ', ' + sellIndex + '}');
  296. }
  297. analyzePrices([1,2,3,4,5,6,7,8,9,0]);
  298. analyzePrices([8,4,3,1,5,5,2,8,2,3]);
  299. */
  300. //----------------------------------------------------
  301. //Exercise: Fizz Buzz
  302. //----------------------------------------------------
  303. /*
  304. //prints numbers from 1 to n, checks if the remainder of i / 3, i / 5, = 0,
  305. // if so, prints fizz and buzz accordingly
  306. function fizzBuzz(n) {
  307. if (n <= 0) {
  308. return '';
  309. }
  310. for (var i = 1; i < n; i++) {
  311. if ((i % 3 === 0) && (i & 5 === 0)) {
  312. console.log(i + 'fizzbuzz');
  313. } else if (i % 3 === 0) {
  314. console.log(i + 'fizz');
  315. } else if (i % 5 === 0) {
  316. console.log(i + 'buzz');
  317. } else {
  318. console.log(i);
  319. }
  320. }
  321. }
  322.  
  323. console.log(fizzBuzz(100));
  324. console.log(fizzBuzz(-5));
  325. */
  326.  
  327. //----------------------------------------------------
  328. //Exercise: Car
  329. //----------------------------------------------------
  330. /*
  331. class Car {
  332. constructor() {
  333. this._speed = 0;
  334. }
  335.  
  336. getSpeed() {
  337. return this._speed;
  338. }
  339.  
  340. setSpeed(speed) {
  341. this._speed = speed;
  342. }
  343.  
  344. stop() {
  345. this._speed = 0;
  346. }
  347. }
  348. var car = new Car();
  349. console.log(car.getSpeed());
  350. car.setSpeed(10);
  351. console.log(car.getSpeed());
  352. car.stop();
  353. console.log(car.getSpeed());
  354. */
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365. </script></body>
  366. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement