My implementation of the "Caesars Cipher" algorithm is marked down as wrong even though it produces the desired output

I’m trying to submit my solution to the problem but it keeps getting marked as wrong. Frustrated by this, I tried using the basic solution available in the ‘Get a Hint!’ section, which produced the same output as mine but got marked as right, for some reason. My code is the following:

function rot13(str) { // LBH QVQ VG!
  var arr = str.split('');
  console.log("\n" + str + ": ");
  arr = arr.map.call(str, function(x) {
    var code = x.charCodeAt(0); 

      if (code >= 65 && code <= 77) { // 77 = 90 - 13
        console.log(x + " = " + String.fromCharCode(code + 13));
        return String.fromCharCode(code + 13);
      }
      else if (code >= 65 && code <= 90) {
        console.log(x + " = " + String.fromCharCode(code -13));
        return String.fromCharCode(code - 13);
      }
      else {
        console.log(String.fromCharCode(code) + " = " + String.fromCharCode(code));
        return String.fromCharCode(code);
      }
    }).join('');
  }


// Change the inputs below to test
rot13("SERR PBQR PNZC");

I’m using Firefox and checking my solution using the Javascript console, if that’s any help.

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

markdown_Forums

1 Like

Your function does not return anything.

Oh my, I forgot the ‘return’ statement. I feel so stupid now. Thanks for your help :slight_smile: