How to get the reciprocal of the letters in a string with TypeScript

1 Answer

0 votes
function get_reciprocal(s: string) { 
    let tmp: string = "";
    const len: number = s.length;
            
    for (let i: number = 0; i < len; i++) { 
       if (!s[i].match(/^[A-Za-z]+$/)) {
          tmp += s[i];
       }
       else { 
           if (s[i] === s[i].toUpperCase()) { 
               tmp += String.fromCharCode('Z'.charCodeAt(0) - s[i].charCodeAt(0) + 'A'.charCodeAt(0));
           } 
           else { 
               if (s[i] === s[i].toLowerCase()) { 
                   tmp += String.fromCharCode('z'.charCodeAt(0) - s[i].charCodeAt(0) + 'a'.charCodeAt(0));
               }     
          }
       }
    } 
    
    return tmp;
}
  
   
let s: string = "abc++deh";
           
s = get_reciprocal(s); 
  
console.log(s);   



/*
run:
  
"zyx++wvs" 
           
*/

 



answered Oct 15, 2024 by avibootz

Related questions

1 answer 118 views
1 answer 104 views
1 answer 93 views
1 answer 102 views
1 answer 95 views
1 answer 104 views
...