Advertisement
vladovip

Characters in Range

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