This Caesar Cipher is killing me!

I’ve been working on this for two days, reeeally trying not to cheat! I’m totally stuck, with no idea what there is left to do.

Spoiler: Hopefully Not Horrible Code
function rot13(str) {
  return str.split('').map(str, function(i) {
    if (i.charCodeAt(0) < 65 || i.charCodeAt(0) > 90) {
      return String.fromCharCode(i);
    } else if (i.charCodeAt(0) < 78) {
      return String.fromCharCode(i + 13);
    } else {
      return String.fromCharCode(i - 13);
    }
  }).join('');
}

I’m really confused and I’d appreciate any tips or hints!

Hey there – take another look at what you’re feeding into String.fromCharCode in your return statements.

A couple of things:

First, it looks like you’re using map incorrectly. The documentation here shows this syntax:

var new_array = arr.map(callback[, thisArg])

Basically, you should be passing just the function into map. No str necessary.


The second thing has to do with this bit of code:

return String.fromCharCode(i + 13)

What is i equal to? (Hint - it’s not a number.)

Thanks, you two! I made a variable charCode for the repeated code i.charCodeAt(0) and put it in all the right places. Thanks for the help!

Spoiler: New (Passing!) Code
function rot13(str) { // LBH QVQ VG!
  return str.split('').map(function(i) {
    var charCode = i.charCodeAt(0);
    if (charCode < 65 || charCode > 90) {
      return i;
    } else if (charCode < 78) {
      return String.fromCharCode(charCode + 13);
    } else {
      return String.fromCharCode(charCode - 13);
    }
  }).join('');
}