Ceaser Cipher doesn't work- Resolved! Thanks to Marmiz & ieahleen

the result is right but there should be something wrong: someone can help me ?

function rot13(str){
    let n = str.length;
    let str2 = String
    for(i=0; i<=n; i++){
        console.log(str.charAt(i), n, str2);
      switch(str.charAt(i)){
        case "A":
          str2 = str2 + 'N';
          break
        case "B":
          str2 = str2 + 'O';
          break
        case "C":
          str2 = str2 + 'P';
          break
        case "D":
          str2 = str2 + 'Q';
          break
        case "E":
          str2 = str2 + 'R';
          break
        case "F":
          str2 = str2 + 'S';
          break
        case "G":
          str2 = str2 + 'T';
          break
        case "H":
          str2 = str2 + 'U';
          break
        case "I":
          str2 = str2 + 'V';
          break
        case "J":
          str2 = str2 + 'W';
          break
        case "K":
          str2 = str2 + 'X';
          break
        case "L":
          str2 = str2 + 'Y';
          break
        case "M":
          str2 = str2 + 'Z';
          break
        case "N":
          str2 = str2 + 'A';
          break
        case "O":
          str2 = str2 + 'B';
          break
        case "P":
          str2 = str2 + 'C';
          break
        case "Q":
          str2 = str2 + 'D';
          break
        case "R":
          str2 = str2 + 'E';
          break
        case "S":
          str2 = str2 + 'F';
          break
        case "T":
          str2 = str2 + 'G';
          break
        case "U":
          str2 = str2 + 'H';
          break
        case "V":
          str2 = str2 + 'I';
          break
        case "W":
          str2 = str2 + 'J';
          break
        case "X":
          str2 = str2 + 'K';
          break
        case "Y":
          str2 = str2 + 'L';
          break
        case "Z":
          str2 = str2 + 'M';
          break
        case ' ':
          str2 = str2 + ' ';
          break   
      }
   }
    str2 = String
    return str2
}
rot13("SERR PBQR PNZC")

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Sorry i am new here. I will learn and follow your suggestions:)

1 Like

Hey @marcocbnet, this here:

let str2 = String

in JS is returning the function constructor for the String object.

Therefore you have two options: either initialize str2 as a string primitive or you call the String() constructor:

str2 = ""; // primitive
str2 = new String(""); // constructor

I would argue the the primitive is what you most commonly find in JavaScript.

Finally, be careful since at the end of your function you are overwriting whatever happened to str2 and returning that:

    str2 = String
    return str2

Hope it helps :sparkles:

i have already tryed the primitive but it didn’t work
now i try with the constructor but it shows me an error
i cancelled the last str2= String
thank you anyway for your help :slight_smile:

You can’t use String, it’s a function.

So if it’s say “HI”:

  1. str2 is the function String
  2. index 0: concatenates “U” onto the function String. This can’t be done, so error.

If you fix that, next issue is that you are using <= in the loop, so it tries to go from 0 to the length of the string. But strings are zero-indexed, the last index is one less than the length.

  1. str2 is an empty string
  2. index 0: concatenate “U” onto the string str2
  3. index 1: concatenate “V” into the string str2
  4. index 2: nothing there, so concatenate undefined onto the string str2. This can’t be done, so error.

Then if you fix that, you’re throwing away whatever you’ve done in the loop by assigning the function String to str2 once you’re finished, so the return value will always be that function

@DanCouper
i am sory if i say something stupid but i am really new.
i read everything you wrote but i don’t have any kind of error. everything works fine. If you console.log(str2) at the end you will have the right result. “FREE CODE CAMP”
but i understand i made an error in the declaration of the variabile because i can’t use it as a result

I don’t understand how it could possibly work: in the loop, you build a string. Then right at the bottom, that first line there, str2 = String overwrites everythng you’ve done.

Instead of returning the string you’ve built, you’re returning a function String, you’re just blowing away everything you did

You can put a console log in before that, and sure, it will show the string you’ve built, but the actual function can’t work

@DanCouper
I am really sorry. that was my last tryed. if you cancel that and console.log(str2) you have the right result but the test doesn’t accept it as answer and i can’t understand why.

do you still have the return statement?

can you post your last code?

@DanCouper

function rot13(str){
    let n = str.length;
    let str2 = "";
    
    for(i=0; i<=n; i++){
        console.log(str.charAt(i), n, str2)
      switch(str.charAt(i)){
        case "A":
          str2 = str2 + 'N';
          break
        case "B":
          str2 = str2 + 'O';
          break
        case "C":
          str2 = str2 + 'P';
          break
        case "D":
          str2 = str2 + 'Q';
          break
        case "E":
          str2 = str2 + 'R';
          break
        case "F":
          str2 = str2 + 'S';
          break
        case "G":
          str2 = str2 + 'T';
          break
        case "H":
          str2 = str2 + 'U';
          break
        case "I":
          str2 = str2 + 'V';
          break
        case "J":
          str2 = str2 + 'W';
          break
        case "K":
          str2 = str2 + 'X';
          break
        case "L":
          str2 = str2 + 'Y';
          break
        case "M":
          str2 = str2 + 'Z';
          break
        case "N":
          str2 = str2 + 'A';
          break
        case "O":
          str2 = str2 + 'B';
          break
        case "P":
          str2 = str2 + 'C';
          break
        case "Q":
          str2 = str2 + 'D';
          break
        case "R":
          str2 = str2 + 'E';
          break
        case "S":
          str2 = str2 + 'F';
          break
        case "T":
          str2 = str2 + 'G';
          break
        case "U":
          str2 = str2 + 'H';
          break
        case "V":
          str2 = str2 + 'I';
          break
        case "W":
          str2 = str2 + 'J';
          break
        case "X":
          str2 = str2 + 'K';
          break
        case "Y":
          str2 = str2 + 'L';
          break
        case "Z":
          str2 = str2 + 'M';
          break
        case ' ':
          str2 = str2 + ' ';
          break
        
     }
    }
    console.log(str2)
    return str2
}
rot13("SERR PBQR PNZC")


as you can see the answer is correct as the test ask but it is not accepted

You should also fixed the unitialized i variable here:

for(i=0; i<=n; i++)

Finally, what about non-letters?
As in: rot13("SERR CVMMN!")
You return FREE PIZZA without !

Hope it helps :slight_smile:

@Marmiz
sorry, if i pasted a wrong attemp.
i would like to work on the “non letter” but it not usefull till i don’t understand why the first result it’s wrong.
run my js and see the result of the first FREE CODE CAMP.

Apologize if I was not clear @marcocbnet,
(once fixed the i variable) your code run fine for that specific case, but this doesn’t mean it’s enough to pass all the requirements set for the challenge.

You are really near to solution, try with the other function calls:

rot13("SERR PBQR PNZC"); // expected FREE CODE CAMP
rot13("SERR CVMMN!") // expected FREE PIZZA! (missing !)
rot13("SERR YBIR?") // expected FREE LOVE? (missing ?)
rot13("GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT.") // expected THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG. (missing .)

@Marmiz the fact is
if you run the code, the software give me wrong even on FREE CODE CAMP
it means i am wrong with the variable not with the procedure.
@ilenia
i would love to work on the rest but the software when i run my program says i am wrong even in FREE CODE CAMP. it means i am wrong with the type of variable not with the procedure.

@ilenia
i would love to work on the rest but the software when i run my program says i am wrong even in FREE CODE CAMP. it means i am wrong with the type of variable not with the procedure.

By software you mean the challenge page from freeCodeCamp?
It’s hard to help you without having a clear picture.

For example in your for loop you have the variable i that never gets initialized:

for(i=0; i<=n; i++)

If you are running it in “strict mode” (freeCodeCamp does it, for instance) will produce an error:

ReferenceError: i is not defined

That can be solved by easily initialized the variable i:

for(let i=0; i<=n; i++)

@Marmiz
yes, i mean the challenge page. my js run with no errors. the result is FREE CODE CAMP
but the page of the challenge says it wrong.

what appears in the console?