Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function reverse(input) {
  2.     if (!input) {
  3.         return [];
  4.     }
  5.  
  6.     if (input.length <= 1) {
  7.         return [...input];
  8.     }
  9.  
  10.     let start = 0;
  11.     let end = 0;
  12.  
  13.     const result = [];
  14.  
  15.     for (let i = 1; i < input.length; i++) {
  16.         if (isWordLetter(input[i])) {
  17.             end++;
  18.             continue;
  19.         }
  20.  
  21.         for (let j = end; j >= start; j--) {
  22.             result.push(input[j]);
  23.         }
  24.  
  25.         while(input[i] && !isWordLetter(input[i])) {
  26.             result.push(input[i]);
  27.             i++;
  28.         }
  29.  
  30.         start = i;
  31.         end = i;
  32.     }
  33.  
  34.     if (result.length < input.length) {
  35.         let index = end;
  36.         while (index >= start) {
  37.             result.push(input[index]);
  38.             index--;
  39.         }
  40.     }
  41.  
  42.     return result;
  43. }
  44.  
  45. function isWordLetter(symbol) {
  46.     return isLowercaseLetter(symbol) || isUppercaseLetter(symbol) || isNumber(symbol);
  47. }
  48.  
  49. function isLowercaseLetter(symbol) {
  50.     const code = symbol.charCodeAt(0);
  51.  
  52.     return code >= 97 && code <= 122;
  53. }
  54.  
  55. function isUppercaseLetter(symbol) {
  56.     const code = symbol.charCodeAt(0);
  57.  
  58.     return code >= 65 && code <= 90;
  59. }
  60.  
  61. function isNumber(symbol) {
  62.     const code = symbol.charCodeAt(0);
  63.  
  64.     return code >= 48 && code <= 57;
  65. }
  66.  
  67. console.log('a:', 'a'.charCodeAt(0));
  68. console.log('z:', 'z'.charCodeAt(0));
  69. console.log('A:', 'A'.charCodeAt(0));
  70. console.log('Z:', 'Z'.charCodeAt(0));
  71. console.log('0:', '0'.charCodeAt(0));
  72. console.log('9:', '9'.charCodeAt(0));
  73.  
  74. const demoInput = ['I',' ', ' ', ' ', 'L','o','v','e',' ',' ',' ','N','u','O','R','D','E','R','!'];
  75. console.log('demoInput:', reverse(demoInput));
  76.  
  77. const singleWordInput = ['N','u','O','R','D','E','R','N','u','O','R','D','E','R'];
  78. console.log('singleWordInput:', reverse(singleWordInput));
  79.  
  80. const whitespacesInput = [' ','"',' ', '@',' ','!'];
  81. console.log('whitespacesInput:', reverse(whitespacesInput));
  82.  
  83. console.log('no input', reverse());
  84.  
  85. const singleLetterInput = ['a'];
  86. console.log('singleLetterInput', reverse(singleLetterInput));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement