Code works elsewhere but not in fCC

Tell us what’s happening:
Describe your issue in detail here.
This is not the first time code has worked in VSCode, in local server, and using console.log in fCC’s own code window… but when I change console.log() to return, the test doesn’t pass.
What gives…?

  **Your code so far**

function rot13(str) {
const theCode = [];   
var az;
for (let i = 0; i <= str.length; i++) {
az = str.charCodeAt(i);
if (az < 65) {
  theCode.push(String.fromCharCode(az));
} else {
  az += 13;
  if (az > 90) {
    az = (az - 90) + 64;
    theCode.push(String.fromCharCode(az));
  } else {
    theCode.push(String.fromCharCode(az));
  }
}
}
console.log(theCode.join(''));
}

rot13("SERR PBQR PNZC");
rot13("SERR CVMMN!");
rot13("SERR YBIR?");
rot13("GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT.");
  **Your browser information:**

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

Challenge: Caesars Cipher

Link to the challenge:

I would argue that this isn’t working anywhere because it isn’t meeting the requirements.

Put this at the bottom of your code and see why:

console.log('what my function returned ->', rot13("SERR PBQR PNZC"));

You’d argue but VSCode doesn’t seem to have a problem… and I get expected output

You got the output to the terminal. That was not the purpose of the challenge. I’m not saying it doesn’t output the correct string, I’m saying that it doesn’t meet the requirements:

Write a function which takes a ROT13encoded string as input and returns a decoded string.

Reread that, paying special attention to the last 4 words. Then please add the test code I suggested.

1 Like

So, instead of
console.log(theCode.join(’’));

I use
return theCode.join(’’));

Is that what you’re saying?

I’m saying that the challenge requires you to return the correct data to fulfill the requirements. It is not doing that. You are suggesting a way to fix that. Why not try it out?

Mate, in my first post, I said when I change console.log to return, it doesn’t pass…
I supplied it as a decoded string

I would suggest you return the value out and log it using JSON.stringify and do a length check as well

console.log(JSON.stringify(rot13("SERR PBQR PNZC")));
console.log("SERR PBQR PNZC".length);
console.log(rot13("SERR PBQR PNZC").length);
2 Likes

I was referring to the code you provided. I’m not looking for an Ikea, “build the code that MiniClaw is thinking about” project.

But yes, with that return statement it still fails.

I was trying to give you some debugging techniques. Why is your string not passing even though it looks the same? If I apply debugging techniques a little further, I would add:

console.log(theCode)

on the line before your return statement. That make it more clear what the issue is.

If things aren’t making sense, start working backwards to find where what you expect and what is happening are diverging.

And just to be clear, you can have both. You don’t have to remove the return to add the log. You can just add a log before the return.

You have an extra character at the end of your array.
image

I’m not too sure why it’s there, I would have to peruse the code better, but if you pop the final character from the array, the test will pass:

theCode.pop()
return theCode.join("")

Cheers … that did the trick. Null character… all sorted

It’s not all sorted. I would want to figure out why it is not working correctly. It is a bad habit for a developer to get into saying, “I have no idea why it broken, but this bandaid seems to make it work.”

I suspect that you may want to have a close look at this line:

for (let i = 0; i <= str.length; i++) {

Details matter.

3 Likes

That’s where I found it was adding the null … adjusted to str.length - 1 resolved the issue

You can also use i < str.length. That would be the conventional fix.

True. Brain f.rt thatI didn’t see until I viewed the array as an unjoined one… and then clarity. There was a little u0000 null character at the end of every sentence…
Good thing I don’t have hair to pull out