1

I created a function to encode or decode messages below. I am struggling with finding the error in my code however. I have the function caesar(str, num) that is moving the letters of the alphabet (in str) over one place by (num). For example when I input caesar('Aaa', 1) I expect 'Bbb' in return, but with my function I am getting 'BBbb'. And for example when I input caesar('AAAAaaaa',1) I get 'BBBBBBBBbbbb'. Not sure why the upper cases are repeating and printing twice but the lower cases are fine. Thanks for any help.

const caesar = function(str, num) {
    let secret = '';
    for ( let i = 0; i < str.length; i++) {
        let index = str.charCodeAt(i);
        if (index >= 65 && index <= 90) {
            secret += String.fromCharCode(index + num);
        } else (index >= 97 && index <= 122)  
            secret += String.fromCharCode(index + num);
          }
          return secret;
}
console.log(caesar('Aaa', 1));
rueeazy
  • 119
  • 1
  • 9

0 Answers0