Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.59 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. // Name: Payton Dudley
  12. // Exercise: Anagram Tester
  13.  
  14. function areTheseAnagrams(word1, word2) {
  15.  
  16. var changeWord1 = word1.replace(/[^A-Za-z]+/g, '').toLowerCase();
  17. var changeWord2 = word2.replace(/[^A-Za-z]+/g, '').toLowerCase();
  18.  
  19. var counts = [];
  20. var word1Length = changeWord1.length;
  21. var word2Length = changeWord2.length
  22.  
  23. if (word1Length !== word2Length) {
  24. return false;
  25. }
  26.  
  27. for (var i = 0; i < word1Length; i++) {
  28. var index = changeWord1.charCodeAt(i)-97;
  29. counts[index] = (counts[index] || 0) + 1;
  30. }
  31.  
  32. for (var i = 0; i < word2Length; i++) {
  33. var index = changeWord2.charCodeAt(i)-97;
  34. if (!counts[index]) {
  35. return false;
  36. } else {
  37. counts[index]--;
  38. }
  39. }
  40.  
  41. return true;
  42. }
  43.  
  44. console.log(areTheseAnagrams("abc","bca"));
  45. console.log(areTheseAnagrams("abc","cde"));
  46.  
  47.  
  48.  
  49. // Name: Payton Dudley
  50. // Exercise: Sum Nested Arrays
  51.  
  52. function sumNested(i) {
  53. var sum=0;
  54. for(var a=0;a<i.length;a++){
  55. if(typeof i[a]=="number"){
  56. sum+=i[a];
  57. }else if(i[a] instanceof Array){
  58. sum+=arraySum(i[a]);
  59. }
  60. }
  61. return sum;
  62. }
  63.  
  64.  
  65. // Name: Payton Dudley
  66. // Exercise: Word Count
  67.  
  68. function wordCount(str) {
  69. var count = 0;
  70. for (var i = 1; i <= str.length; i++) {
  71. if (str.charAt(i) == " ") {
  72. count ++;
  73. }
  74. }
  75. return count + 1;
  76. }
  77. console.log(wordCount("This is the sentence I want to test my word count on"));
  78.  
  79.  
  80. // Name: Payton Dudley
  81. // Exercise: Property Path Evaluation
  82.  
  83. /*function propertyValueAt(key, obj) {
  84. return key.split('.').reduce(function(a,b){
  85. return a && a[b];
  86. }, obj);
  87. }
  88. propertyValueAt('foo.bar', obj);
  89.  
  90. var key = key.split('.');
  91. console.log(obj[keys[0]][keys[1]]);
  92.  
  93. var object = {
  94. a: 1,
  95. b: {
  96. c: 2,
  97. d: 3
  98. }
  99. };
  100. propertyValueAt(object,['a']);*/
  101.  
  102. // Name: Payton Dudley
  103. // Exercise: Object Oriented Programming - Car
  104.  
  105. function Car() {
  106. this.speed = 0;
  107. this.getSpeed = function () {
  108. return this.speed;
  109. }
  110.  
  111.  
  112. this.setSpeed = function (speed) {
  113. if (speed >= 0) {
  114. this.speed = speed;}
  115. }
  116.  
  117. this.stop = function () {
  118. this.speed = 0;
  119. }
  120.  
  121. }
  122.  
  123. var myCar = new Car();
  124. console.log(myCar.getSpeed());
  125. myCar.setSpeed(25);
  126. console.log(myCar.getSpeed());
  127. myCar.stop();
  128. console.log(myCar.getSpeed());
  129.  
  130. // Name: Payton Dudley
  131. // Exercise: Analyze Prices
  132.  
  133. var prices = [1,2,3,4,5,6,7,8,9,0];
  134. var min = 0;
  135. var buyIndex;
  136. var sellIndex;
  137. var obj = {buyIndex: Number.POSITIVE_INFINITY, sellIndex: Number.NEGATIVE_INFINITY};
  138.  
  139. function analyzePrices(prices) {
  140. for (var i=prices.length-1; i>=0; i--) {
  141. tmp = prices[i];
  142. if (tmp < buyIndex) buyIndex = tmp;
  143. if (tmp > sellIndex) sellIndex = tmp;
  144. }
  145. var obj ={}
  146. obj[buyIndex];
  147. return obj{buyIndex, sellIndex};
  148. }
  149. analyzePrices(prices);
  150.  
  151. // Name: Payton Dudley
  152. // Exercise: Fizz Buzz
  153.  
  154. var n = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
  155. function fizzbuzz (n) {
  156. for(var i = 1; i < n.length; i++){
  157. if( i % 3 === 0 && i % 5 === 0){
  158. console.log(i + "fizzbuzz");
  159. }else if( i % 3 === 0){
  160. console.log(i + "fizz");
  161. }else if( i % 5 === 0){
  162. console.log(i + "buzz");
  163. } else {
  164. console.log(i);
  165. }
  166. }
  167. }
  168. console.log(fizzbuzz(n));
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201. </script>
  202.  
  203.  
  204.  
  205. <script id="jsbin-source-javascript" type="text/javascript">// Name: Payton Dudley
  206. // Exercise: Anagram Tester
  207.  
  208. function areTheseAnagrams(word1, word2) {
  209.  
  210. var changeWord1 = word1.replace(/[^A-Za-z]+/g, '').toLowerCase();
  211. var changeWord2 = word2.replace(/[^A-Za-z]+/g, '').toLowerCase();
  212.  
  213. var counts = [];
  214. var word1Length = changeWord1.length;
  215. var word2Length = changeWord2.length
  216.  
  217. if (word1Length !== word2Length) {
  218. return false;
  219. }
  220.  
  221. for (var i = 0; i < word1Length; i++) {
  222. var index = changeWord1.charCodeAt(i)-97;
  223. counts[index] = (counts[index] || 0) + 1;
  224. }
  225.  
  226. for (var i = 0; i < word2Length; i++) {
  227. var index = changeWord2.charCodeAt(i)-97;
  228. if (!counts[index]) {
  229. return false;
  230. } else {
  231. counts[index]--;
  232. }
  233. }
  234.  
  235. return true;
  236. }
  237.  
  238. console.log(areTheseAnagrams("abc","bca"));
  239. console.log(areTheseAnagrams("abc","cde"));
  240.  
  241.  
  242.  
  243. // Name: Payton Dudley
  244. // Exercise: Sum Nested Arrays
  245.  
  246. function sumNested(i) {
  247. var sum=0;
  248. for(var a=0;a<i.length;a++){
  249. if(typeof i[a]=="number"){
  250. sum+=i[a];
  251. }else if(i[a] instanceof Array){
  252. sum+=arraySum(i[a]);
  253. }
  254. }
  255. return sum;
  256. }
  257.  
  258.  
  259. // Name: Payton Dudley
  260. // Exercise: Word Count
  261.  
  262. function wordCount(str) {
  263. var count = 0;
  264. for (var i = 1; i <= str.length; i++) {
  265. if (str.charAt(i) == " ") {
  266. count ++;
  267. }
  268. }
  269. return count + 1;
  270. }
  271. console.log(wordCount("This is the sentence I want to test my word count on"));
  272.  
  273.  
  274. // Name: Payton Dudley
  275. // Exercise: Property Path Evaluation
  276.  
  277. /*function propertyValueAt(key, obj) {
  278. return key.split('.').reduce(function(a,b){
  279. return a && a[b];
  280. }, obj);
  281. }
  282. propertyValueAt('foo.bar', obj);
  283.  
  284. var key = key.split('.');
  285. console.log(obj[keys[0]][keys[1]]);
  286.  
  287. var object = {
  288. a: 1,
  289. b: {
  290. c: 2,
  291. d: 3
  292. }
  293. };
  294. propertyValueAt(object,['a']);*/
  295.  
  296. // Name: Payton Dudley
  297. // Exercise: Object Oriented Programming - Car
  298.  
  299. function Car() {
  300. this.speed = 0;
  301. this.getSpeed = function () {
  302. return this.speed;
  303. }
  304.  
  305.  
  306. this.setSpeed = function (speed) {
  307. if (speed >= 0) {
  308. this.speed = speed;}
  309. }
  310.  
  311. this.stop = function () {
  312. this.speed = 0;
  313. }
  314.  
  315. }
  316.  
  317. var myCar = new Car();
  318. console.log(myCar.getSpeed());
  319. myCar.setSpeed(25);
  320. console.log(myCar.getSpeed());
  321. myCar.stop();
  322. console.log(myCar.getSpeed());
  323.  
  324. // Name: Payton Dudley
  325. // Exercise: Analyze Prices
  326.  
  327. var prices = [1,2,3,4,5,6,7,8,9,0];
  328. var min = 0;
  329. var buyIndex;
  330. var sellIndex;
  331. var obj = {buyIndex: Number.POSITIVE_INFINITY, sellIndex: Number.NEGATIVE_INFINITY};
  332.  
  333. function analyzePrices(prices) {
  334. for (var i=prices.length-1; i>=0; i--) {
  335. tmp = prices[i];
  336. if (tmp < buyIndex) buyIndex = tmp;
  337. if (tmp > sellIndex) sellIndex = tmp;
  338. }
  339. var obj ={}
  340. obj[buyIndex];
  341. return obj{buyIndex, sellIndex};
  342. }
  343. analyzePrices(prices);
  344.  
  345. // Name: Payton Dudley
  346. // Exercise: Fizz Buzz
  347.  
  348. var n = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
  349. function fizzbuzz (n) {
  350. for(var i = 1; i < n.length; i++){
  351. if( i % 3 === 0 && i % 5 === 0){
  352. console.log(i + "fizzbuzz");
  353. }else if( i % 3 === 0){
  354. console.log(i + "fizz");
  355. }else if( i % 5 === 0){
  356. console.log(i + "buzz");
  357. } else {
  358. console.log(i);
  359. }
  360. }
  361. }
  362. console.log(fizzbuzz(n));
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394. </script></body>
  395. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement