Caesars Cipher - so close but not all numbers converting to proper characters?

Caesars Cipher - so close but not all numbers converting to proper characters?
0

#1

Tell us what’s happening:
i seem to be getting close but any hints would be appreciated as to why i am getting the right output but missing only a few letters in the translation i am lost as to where i went wrong and rather get some advise before looking at answers as this is the first day ive been able to start completing code challenges independently and it feels damn good thanks for your time and help
lucas

Your code so far

function rot13(str) { // LBH QVQ VG!
  var encoded = [];
  var i = 0;
  var letters = str.split("");
  while (i < letters.length) {
     Encoded = letters[i].charCodeAt();
     less = Encoded - 13;
     encoded.push(less);
     i++;
  }
  var j = 0;
  while (j < encoded.length) {
      encoded = String.fromCharCode.apply(null, encoded);
      j++;
      return encoded;
  }
//     return encoded;
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36.

Link to the challenge:


#2

The problem with your solution is that you always subtract 13 from Encoded, when sometimes you should be adding 13 to Encoded. You need to figure out when you should subtract and when you should add. For any character which starts off A-Z should still be an A-Z after you convert. The other characters (i.e. spaces, punctuation, etc…) do not need to be converted at all.


#3

Thank you so much for your fast response I’ll see what I can make happen in light of your advice much appreciated !! :slight_smile:


#4

So I did it! :smiley:

function rot13(str) { // LBH QVQ VG!
  var encoded = [];
  var i = 0;
  var letters = str.split("");
  
  while (i < letters.length) {
     Encoded = letters[i].charCodeAt(); 
     less = Encoded - 13;
     more = Encoded + 13;
   
    if (Encoded >= 78){
       encoded.push(less);  
    } else if (Encoded < 78 && Encoded > 64) {
      encoded.push(more);
    } else if (Encoded < 64) {
      encoded.push(Encoded);
    }
     i++;
  }
// return encoded;  
  var j = 0;
  while (j < encoded.length) {
      encoded = String.fromCharCode.apply(null, encoded);
      j++;
      return encoded;
}
    return encoded;
}

// Change the inputs below to test
rot13("SERR CVMMN!");

#5

Congrats on solving the challenge. See below where I took your code and simplified it a bit. You did not need to create the letters array, because you can access a specific string character with a reference to it’s index just like you do with arrays. Also, there was no need to create the extra while loop, because you just need to apply the fromCharCode function to encoded and assign it back to encoded. I eliminated that extra step by just returning the result of String.fromCharCode.apply(null, encoded).

function rot13(str) { // LBH QVQ VG!
  var encoded = [];
  var i = 0;
  while (i < str.length) {
    Encoded = str[i].charCodeAt(); 
    if (Encoded >= 65 && Encoded <= 90) {
      if (Encoded > 77) {
        Encoded -= 13;
      } 
      else {
        Encoded += 13;
      }
    }
    encoded.push(Encoded);
    i++;
  }
  return String.fromCharCode.apply(null, encoded);
}

The above code can be simplified a further by replacing the if/else statement with the use of the ternary operator.

function rot13(str) { // LBH QVQ VG!
  var encoded = [];
  var i = 0;
  while (i < str.length) {
    Encoded = str[i].charCodeAt(); 
    if (Encoded >= 65 && Encoded <= 90) {
      Encoded += Encoded > 77 ? -13 : 13;
    }
    encoded.push(Encoded);
    i++;
  }
  return String.fromCharCode.apply(null, encoded);
}

#6

FYI - I’ve edited your code for readability. When you enter a code block into the forum, 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.