Advertisement
miroLLL

Characters in Range - Miro's version

Jun 12th, 2020
1,120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solution(firstChar, lastChar) {
  2.  
  3.     let first = getNumberValue(firstChar);
  4.     let last = getNumberValue(lastChar);
  5.  
  6.     let min = Math.min(first, last);
  7.     let max = Math.max(first, last);
  8.  
  9.     let allSymbols = getCharsInRange(min, max);
  10.  
  11.     console.log(joinSymbols(allSymbols, " "));
  12.  
  13.     function getNumberValue(char) {
  14.         return char.charCodeAt(0);
  15.     }
  16.  
  17.     function getCharValue(number) {
  18.         return String.fromCharCode(number);
  19.     }
  20.  
  21.     function getCharsInRange(start, end) {
  22.         let characters = [];
  23.  
  24.         for (let i = (start + 1); i < end; i++) {
  25.             let symbol = getCharValue(i);
  26.             characters.push(symbol);
  27.         }
  28.  
  29.         return characters;
  30.     }
  31.  
  32.     function joinSymbols(arr, separator) {
  33.         let result = "";
  34.         for (let index in arr) {
  35.             let character = arr[index];
  36.  
  37.             if (index <= (arr.length - 2)) {
  38.                 result += `${character}${separator}`;
  39.             } else {
  40.                 result += character;
  41.             }
  42.         }
  43.         return result;
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement