Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env node
  2.  
  3. function reverse_int(int){
  4.     let ret = 0;
  5.  
  6.     for (let i = 0; int !== 0 ; ++i) {
  7.         ret+=int%10;
  8.         // parseInt is a casting
  9.         int=parseInt(int/10);
  10.         ret*=10;
  11.     }
  12.  
  13.     return ret;
  14. }
  15.  
  16. function is_palindrome(string){
  17.     // sed syntax
  18.     string=string.replace(/\s/g, "");
  19.  
  20.     let len = string.length - 1;
  21.     for (let i = 0; i < len ; ++i) {
  22.         if(string[i] !== string[len-i]) return false;
  23.     }
  24.     return true;
  25. }
  26.  
  27. function alpha_sort(string){
  28.  
  29.     string=string.replace(/\s/g, "");
  30.  
  31.     let arr = Array.from(string);
  32.     arr = arr.sort();
  33.     return arr.join("");
  34. }
  35.  
  36. function uppercase_words(string){
  37.     // No argument defaults to space, argument is given for clarity
  38.     let arr = string.split(" ");
  39.     for (let i=0; i<arr.length; ++i) {
  40.         arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].substring(1);
  41.     }
  42.     return arr.join(" ");
  43. }
  44.  
  45. function longest_word(string){
  46.     let arr = string.split(" ");
  47.     let max=0;
  48.     for(let i=1; i<arr.length; ++i){
  49.         if(arr[i].length>arr[max].length) max=i;
  50.     }
  51.     return arr[max];
  52. }
  53.  
  54. function vowel_count(string){
  55.     let ret = 0;
  56.     for(let i=0; i<string.length; ++i){
  57.         switch (string.charAt(i)) {
  58.             case 'a':
  59.             case 'e':
  60.             case 'i':
  61.             case 'o':
  62.             case 'u':
  63.                 ++ret;
  64.         }
  65.     }
  66.     return ret;
  67. }
  68.  
  69. function is_prime(int){
  70.     for(let i=parseInt(Math.sqrt(int)); i>1; --i){
  71.         if(int%i === 0) return false;
  72.     }
  73.     return true;
  74. }
  75.  
  76. // 1
  77. let int_example = 123456789;
  78. console.log("Reversing " + int_example + ": " + reverse_int(int_example));
  79.  
  80. // 2
  81. let palindrome_example = "nurses run";
  82. console.log("Checking \"" + palindrome_example + "\": " + is_palindrome(palindrome_example));
  83.  
  84. // 3
  85. let alpha_example="zyxwvutsrqponmlkjihgfedcba";
  86. console.log("Alphabetically sorting \"" + alpha_example + "\": " + alpha_sort(alpha_example));
  87.  
  88. // 4
  89. let upper_example = "the quick brown fox";
  90. console.log("Parsing \"" + upper_example + "\": " + uppercase_words(upper_example));
  91.  
  92. // 5
  93. let longest_example = "Web Development Tutorial";
  94. console.log("Parsing \"" + longest_example + "\": " + longest_word(longest_example));
  95.  
  96. // 6
  97. let vowel_example = "the quick brown fox";
  98. console.log("Counting vowels in \"" + vowel_example + "\": " + vowel_count(vowel_example));
  99.  
  100. // 7
  101. let prime_example = 274876858367;
  102. console.log(is_prime(prime_example));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement