Free Code Camp - caesars-cipher

function rot13(str) {
  var obj ={"N": "A" ,"O":"B","P":"C","Q":"D","R":"E","S":"F","T":"G","U":"H","V":"I","W":"J","X":"K","Y":"L","Z":"M" }
  var obj2 ={ "A":"N" ,"B":"O","C":"P","D":"Q","E":"R","F":"S","G":"T","H":"U","I":"V","J":"W","K":"X","L":"Y","M":"Z","!":"!",".":".","?":"?" }
// ABCDEFGHIJKLM
// NOPQRSTUVWXYZ
var str2=''
for (var i =0; i<=str.length;i++){

str2+=obj[str[i]]===undefined?obj2[str[i]]:obj[str[i]];


}
console.log(str2.replace(/undefined/g," "),str)

return str2.replace(/undefined/g," ")
}


rot13("SERR PBQR PNZC");

So when i console this the outputs seem fine in console and it converts all cipher into translated english but the issue is the challenge is not completed

Challenge Link:-

Please post your actual code instead of a picture of your code. It is very difficult to debug pictures.

i changed picture to code

I put your code through a formatter to help readability.

function rot13(str) {
  var obj = {
    "N": "A",
    "O": "B",
    "P": "C",
    "Q": "D",
    "R": "E",
    "S": "F",
    "T": "G",
    "U": "H",
    "V": "I",
    "W": "J",
    "X": "K",
    "Y": "L",
    "Z": "M"
  }
  var obj2 = {
    "A": "N",
    "B": "O",
    "C": "P",
    "D": "Q",
    "E": "R",
    "F": "S",
    "G": "T",
    "H": "U",
    "I": "V",
    "J": "W",
    "K": "X",
    "L": "Y",
    "M": "Z",
    "!": "!",
    ".": ".",
    "?": "?"
  }
  // ABCDEFGHIJKLM
  // NOPQRSTUVWXYZ
  var str2 = ''
  for (var i = 0; i <= str.length; i++) {
    str2 += obj[str[i]] === undefined ? obj2[str[i]] : obj[str[i]];
  }
  console.log(str2.replace(/undefined/g, " "), str)

  return str2.replace(/undefined/g, " ")
}

// Take a look at the output. What do you see?
console.log("|-----|" + rot13("SERR PBQR PNZC") + "|-----|");

Take a look at the output from the test case. In general, if you are replacing 'undefined', this is an indication that you are doing something unwise.

Side note: you should use let and const instead of var.

its out put is |-----|FREE CODE CAMP |-----|
inxpect console

Right. So why would that output be an indication of a problem? What should "SERR PBQR PNZC" become? You have an extra character at the end.

well its output is fine and is converting the ciphers but the challenge isn’t suceeding
what is extra character in it?

if u are refering to ? it is for the sentence SERR YBIR?

Your output is not fine.

The first test case says

rot13("SERR PBQR PNZC") should decode to the string FREE CODE CAMP

Your code returns |—|FREE CODE CAMP <—|. There is a very small difference.

ok thanks i used trim and it worked thank you for helping :smiling_face_with_three_hearts:

Good job seeing it!

I would strongly recommend reworking your solution to avoid creating ‘undefined’ anywhere in your logic. That is an indication that something is going wrong.

ok i will try to change that statement

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.