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

1 Answer

0 votes
function get_reciprocal(s) { 
    let tmp = "";
    const len = s.length;
            
    for (let i = 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 = "abc++def";
           
s = get_reciprocal(s); 
  
console.log(s);   



/*
run:
  
zyx++wvu
           
*/

 



answered Jan 19, 2020 by avibootz
edited 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
...