Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function reverse(input) {
- if (!input) {
- return [];
- }
- if (input.length <= 1) {
- return [...input];
- }
- let start = 0;
- let end = 0;
- const result = [];
- for (let i = 1; i < input.length; i++) {
- if (isWordLetter(input[i])) {
- end++;
- continue;
- }
- for (let j = end; j >= start; j--) {
- result.push(input[j]);
- }
- while(input[i] && !isWordLetter(input[i])) {
- result.push(input[i]);
- i++;
- }
- start = i;
- end = i;
- }
- if (result.length < input.length) {
- let index = end;
- while (index >= start) {
- result.push(input[index]);
- index--;
- }
- }
- return result;
- }
- function isWordLetter(symbol) {
- return isLowercaseLetter(symbol) || isUppercaseLetter(symbol) || isNumber(symbol);
- }
- function isLowercaseLetter(symbol) {
- const code = symbol.charCodeAt(0);
- return code >= 97 && code <= 122;
- }
- function isUppercaseLetter(symbol) {
- const code = symbol.charCodeAt(0);
- return code >= 65 && code <= 90;
- }
- function isNumber(symbol) {
- const code = symbol.charCodeAt(0);
- return code >= 48 && code <= 57;
- }
- console.log('a:', 'a'.charCodeAt(0));
- console.log('z:', 'z'.charCodeAt(0));
- console.log('A:', 'A'.charCodeAt(0));
- console.log('Z:', 'Z'.charCodeAt(0));
- console.log('0:', '0'.charCodeAt(0));
- console.log('9:', '9'.charCodeAt(0));
- const demoInput = ['I',' ', ' ', ' ', 'L','o','v','e',' ',' ',' ','N','u','O','R','D','E','R','!'];
- console.log('demoInput:', reverse(demoInput));
- const singleWordInput = ['N','u','O','R','D','E','R','N','u','O','R','D','E','R'];
- console.log('singleWordInput:', reverse(singleWordInput));
- const whitespacesInput = [' ','"',' ', '@',' ','!'];
- console.log('whitespacesInput:', reverse(whitespacesInput));
- console.log('no input', reverse());
- const singleLetterInput = ['a'];
- console.log('singleLetterInput', reverse(singleLetterInput));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement