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 JavaScript

2 Answers

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

    let result = '';
    let charcode = 0;
    let size = str.length;

    for (let i = 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, N) {
    let result = "";
    
    for (let char of str) {
        let asciicode = 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('8a Fkq', 3));



/*
run:
 
8d Int
 
*/

 





answered Feb 28 by avibootz
...