Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env node
- function reverse_int(int){
- let ret = 0;
- for (let i = 0; int !== 0 ; ++i) {
- ret+=int%10;
- // parseInt is a casting
- int=parseInt(int/10);
- ret*=10;
- }
- return ret;
- }
- function is_palindrome(string){
- // sed syntax
- string=string.replace(/\s/g, "");
- let len = string.length - 1;
- for (let i = 0; i < len ; ++i) {
- if(string[i] !== string[len-i]) return false;
- }
- return true;
- }
- function alpha_sort(string){
- string=string.replace(/\s/g, "");
- let arr = Array.from(string);
- arr = arr.sort();
- return arr.join("");
- }
- function uppercase_words(string){
- // No argument defaults to space, argument is given for clarity
- let arr = string.split(" ");
- for (let i=0; i<arr.length; ++i) {
- arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].substring(1);
- }
- return arr.join(" ");
- }
- function longest_word(string){
- let arr = string.split(" ");
- let max=0;
- for(let i=1; i<arr.length; ++i){
- if(arr[i].length>arr[max].length) max=i;
- }
- return arr[max];
- }
- function vowel_count(string){
- let ret = 0;
- for(let i=0; i<string.length; ++i){
- switch (string.charAt(i)) {
- case 'a':
- case 'e':
- case 'i':
- case 'o':
- case 'u':
- ++ret;
- }
- }
- return ret;
- }
- function is_prime(int){
- for(let i=parseInt(Math.sqrt(int)); i>1; --i){
- if(int%i === 0) return false;
- }
- return true;
- }
- // 1
- let int_example = 123456789;
- console.log("Reversing " + int_example + ": " + reverse_int(int_example));
- // 2
- let palindrome_example = "nurses run";
- console.log("Checking \"" + palindrome_example + "\": " + is_palindrome(palindrome_example));
- // 3
- let alpha_example="zyxwvutsrqponmlkjihgfedcba";
- console.log("Alphabetically sorting \"" + alpha_example + "\": " + alpha_sort(alpha_example));
- // 4
- let upper_example = "the quick brown fox";
- console.log("Parsing \"" + upper_example + "\": " + uppercase_words(upper_example));
- // 5
- let longest_example = "Web Development Tutorial";
- console.log("Parsing \"" + longest_example + "\": " + longest_word(longest_example));
- // 6
- let vowel_example = "the quick brown fox";
- console.log("Counting vowels in \"" + vowel_example + "\": " + vowel_count(vowel_example));
- // 7
- let prime_example = 274876858367;
- console.log(is_prime(prime_example));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement