Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,037 questions

40,897 answers

573 users

How to shift each letter in a string N places up in the alphabet with TypeScript

2 Answers

0 votes
function shiftEachLetterNPlacesUp(str: string, N: number) {
    str = str.toLowerCase();

    let result: string = '';
    let charcode: number = 0;
    let size: number = str.length;

    for (let i: number = 0; i < size; i++) {
        charcode = (str[i].charCodeAt(0)) + N;
        result += String.fromCharCode(charcode);
    }
    
    return result;
}

console.log(shiftEachLetterNPlacesUp('afkq', 3));




/*
run:
 
"dint"
 
*/

 





answered Feb 28 by avibootz
edited Feb 28 by avibootz
0 votes
function shiftEachLetterNPlacesUp(str: string, N: number) {
   let result: string = "";
    
    for (let char of str) {
        let asciicode: number = char.charCodeAt(0) + N;
        if (asciicode >= 97 && asciicode <= 122 || asciicode >= 65 && asciicode <= 90) {
            result += String.fromCharCode(asciicode);
        } else {
            result += char;
        }
    }
    
    return result;
}

console.log(shiftEachLetterNPlacesUp('9a Fkq', 3));




/*
run:
 
"9d Int" 
 
*/

 





answered Feb 28 by avibootz
...