Can't figure out whats wrong..:(

Tell us what’s happening:
Describe your issue in detail here.

   **Your code so far**

function rot13(str) {
for (var i=0; i<str.length; i++){
 var decodedstring = "";
 var asciivalue = str[i].charCodeAt();
 if (asciivalue >= 65 && asciivalue <= 90){
   decodedstring += String.fromCharCode(asciivalue + 13)
 } else if( asciivalue >= 78 && asciivalue <= 90){
   decodedstring += string.fromCharCode(asciivalue - 13)
 } else {
   decodedstring += str[i];
 }
//console.log(asciivalue)
}
 return decodedstring;
}

console.log(rot13("SERR PBQR PNZC"));
   **Your browser information:**

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0

Challenge: Caesars Cipher

Link to the challenge:

1 Like

You set your decodestring to “” at the start of EVERY LOOP.

Once you fix that, you can at least see what you are doing.

You’re almost there, only a few issues:

Jagaya already mentioned the first one. Next, look at your if statements:

    if (asciivalue >= 65 && asciivalue <= 90) {
      decodedstring += String.fromCharCode(asciivalue + 13)
    } else if (asciivalue >= 78 && asciivalue <= 90) {
      decodedstring += string.fromCharCode(asciivalue - 13)
    } 

What happens if asciivalue is 80 ? It’s never hitting the second condition. 80 is between 65 and 90, so the first if is true.

Once you fix that, your code will go into the second if when it’s supposed to, but you’ll get a small syntax error.

When that is fixed as well, your code passes.

Try

console.log(asciivalue); 
console.log(decodedstring);

to see where you are going wrong :slightly_smiling_face:

Just as an aside. It’s fine to log values inside functions. However, when a function returns a value it is always a good idea to log the return. Sometimes, logs inside functions can mislead you if you do not interpret them correctly.

console.log(rot13("SERR PBQR PNZC")).

1 Like

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