How to generate a range of characters in Node.js

2 Answers

0 votes
function generate_range_of_characters(ch_from, ch_to) {
    let s = String.fromCharCode(...[...Array(ch_to.charCodeAt(0) - 
                                              ch_from.charCodeAt(0) + 1).keys()].
                                              map(i => i + ch_from.charCodeAt(0)));
      
    return s;
}
  
console.log(generate_range_of_characters('e', 'm'));
  
  
  
  
  
/*
run:
  
efghijklm
  
*/

 



answered Jan 27, 2022 by avibootz
0 votes
function generate_range_of_characters(ch_from, ch_to) {
    let s = "";
    for (let i = ch_from.charCodeAt(0); i <= ch_to.charCodeAt(0); i++) {
         s += String.fromCharCode(i);
    }
    return s;
}
  
console.log(generate_range_of_characters('e', 'm'));
  
  
  
  
  
/*
run:
  
efghijklm
  
*/

 



answered Jan 27, 2022 by avibootz

Related questions

...