Guest User

Untitled

a guest
Dec 14th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 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. // returns the longest country name
  12.  
  13. function longestCountryName(country_name) {
  14.  
  15. return country_name.reduce(function(lname,country) {
  16.  
  17. return lname.length > country.length ? lname : country;
  18. },
  19. "");
  20. }
  21.  
  22. console.log(longestCountryName(["Australia", "Germany", "United States of America"]));
  23.  
  24. // checks to see if a number is prime or not
  25.  
  26. function testPrime(n) {
  27. if (n === 1) {
  28. return false;
  29. } else if (n === 2) {
  30. return true;
  31. }else {
  32. for (var x = 2; x < n; x++) {
  33. if (n % x === 0) {
  34. return false;
  35. }
  36. }
  37. return true;
  38. }
  39.  
  40. }
  41.  
  42. console.log(testPrime(31));
  43.  
  44. // returns the sentence with the first letters uppercased
  45.  
  46. function uppercase(str) {
  47. var array1 = str.split(' ');
  48. var newArray = [];
  49.  
  50. for (var x = 0; x < array1.length; x++) {
  51. newArray.push(array1[x].charAt(0).toUpperCase() + array1[x].slice(1));
  52. }
  53. return newArray.join(' ');
  54. }
  55.  
  56. console.log(uppercase("the quick brown fox"));
  57.  
  58. // Accepts a string as a parameter and counts the number of vowels in the string
  59.  
  60. function vowel_count(str) {
  61. var vowel_list = "aeiouAEIOU";
  62. var vcount = 0;
  63.  
  64. for (var x = 0; x < str.length; x++) {
  65. if (vowel_list.indexOf(str[x]) !== -1) {
  66. vcount += 1;
  67. }
  68. }
  69. return vcount;
  70. }
  71.  
  72. console.log(vowel_count("The quick brown fox"));
  73.  
  74. // finds the longest word within a string
  75.  
  76. function findLongestWord(str) {
  77. var arr = str.match(/\w[a-z]{0,}/gi);
  78. var result = arr[0];
  79.  
  80. for (var x = 1; x < arr.length; x++) {
  81.  
  82. if(result.length < arr[x].length) {
  83. result = arr[x];
  84. }
  85. }
  86. return result;
  87. }
  88.  
  89. console.log(findLongestWord('Web Development Tutorial'));
  90.  
  91. // counts the number of occurences of the specified letter within the string
  92.  
  93. function charCount(str,letter) {
  94. var letter_count = 0;
  95. for (var position = 0; position < str.length; position++) {
  96. if (str.charAt(position) === letter) {
  97. letter_count++;
  98. }
  99. }
  100. return letter_count;
  101. }
  102.  
  103. console.log(charCount('doodle mcnoodle', 'o'));
  104.  
  105. // generates a string ID (specified length) of random characters
  106.  
  107. function makeId(l) {
  108. var text = "";
  109. var char_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  110.  
  111. for (var i = 0;i < l; i++) {
  112. text += char_list.charAt(Math.floor(Math.random() * char_list.length));
  113. }
  114. return text;
  115. }
  116.  
  117. console.log(makeId(8));
  118.  
  119. // returns a passed strings with letters in alphabetical order
  120.  
  121. function alphabet_order(str) {
  122.  
  123. return str.split('').sort().join('');
  124. }
  125.  
  126. console.log(alphabet_order("webmaster"));
  127.  
  128.  
  129. //returns only the numbers in the array bigger than the val
  130.  
  131. function BiggerEl(val) {
  132.  
  133. return function(evalue, index, array) {
  134. return (evalue >= val);
  135. };
  136. }
  137. var result = [11,45,4,31,64,10,5,6].filter(BiggerEl(10));
  138. console.log(result);
  139. </script>
  140.  
  141.  
  142.  
  143. <script id="jsbin-source-javascript" type="text/javascript">// returns the longest country name
  144.  
  145. function longestCountryName(country_name) {
  146.  
  147. return country_name.reduce(function(lname,country) {
  148.  
  149. return lname.length > country.length ? lname : country;
  150. },
  151. "");
  152. }
  153.  
  154. console.log(longestCountryName(["Australia", "Germany", "United States of America"]));
  155.  
  156. // checks to see if a number is prime or not
  157.  
  158. function testPrime(n) {
  159. if (n === 1) {
  160. return false;
  161. } else if (n === 2) {
  162. return true;
  163. }else {
  164. for (var x = 2; x < n; x++) {
  165. if (n % x === 0) {
  166. return false;
  167. }
  168. }
  169. return true;
  170. }
  171.  
  172. }
  173.  
  174. console.log(testPrime(31));
  175.  
  176. // returns the sentence with the first letters uppercased
  177.  
  178. function uppercase(str) {
  179. var array1 = str.split(' ');
  180. var newArray = [];
  181.  
  182. for (var x = 0; x < array1.length; x++) {
  183. newArray.push(array1[x].charAt(0).toUpperCase() + array1[x].slice(1));
  184. }
  185. return newArray.join(' ');
  186. }
  187.  
  188. console.log(uppercase("the quick brown fox"));
  189.  
  190. // Accepts a string as a parameter and counts the number of vowels in the string
  191.  
  192. function vowel_count(str) {
  193. var vowel_list = "aeiouAEIOU";
  194. var vcount = 0;
  195.  
  196. for (var x = 0; x < str.length; x++) {
  197. if (vowel_list.indexOf(str[x]) !== -1) {
  198. vcount += 1;
  199. }
  200. }
  201. return vcount;
  202. }
  203.  
  204. console.log(vowel_count("The quick brown fox"));
  205.  
  206. // finds the longest word within a string
  207.  
  208. function findLongestWord(str) {
  209. var arr = str.match(/\w[a-z]{0,}/gi);
  210. var result = arr[0];
  211.  
  212. for (var x = 1; x < arr.length; x++) {
  213.  
  214. if(result.length < arr[x].length) {
  215. result = arr[x];
  216. }
  217. }
  218. return result;
  219. }
  220.  
  221. console.log(findLongestWord('Web Development Tutorial'));
  222.  
  223. // counts the number of occurences of the specified letter within the string
  224.  
  225. function charCount(str,letter) {
  226. var letter_count = 0;
  227. for (var position = 0; position < str.length; position++) {
  228. if (str.charAt(position) === letter) {
  229. letter_count++;
  230. }
  231. }
  232. return letter_count;
  233. }
  234.  
  235. console.log(charCount('doodle mcnoodle', 'o'));
  236.  
  237. // generates a string ID (specified length) of random characters
  238.  
  239. function makeId(l) {
  240. var text = "";
  241. var char_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  242.  
  243. for (var i = 0;i < l; i++) {
  244. text += char_list.charAt(Math.floor(Math.random() * char_list.length));
  245. }
  246. return text;
  247. }
  248.  
  249. console.log(makeId(8));
  250.  
  251. // returns a passed strings with letters in alphabetical order
  252.  
  253. function alphabet_order(str) {
  254.  
  255. return str.split('').sort().join('');
  256. }
  257.  
  258. console.log(alphabet_order("webmaster"));
  259.  
  260.  
  261. //returns only the numbers in the array bigger than the val
  262.  
  263. function BiggerEl(val) {
  264.  
  265. return function(evalue, index, array) {
  266. return (evalue >= val);
  267. };
  268. }
  269. var result = [11,45,4,31,64,10,5,6].filter(BiggerEl(10));
  270. console.log(result);</script></body>
  271. </html>
Add Comment
Please, Sign In to add comment