Caesars Cipher Algorithm Challenge: output seems correct, answer rejected

Caesars Cipher Algorithm Challenge: output seems correct, answer rejected
0

#1

Tell us what’s happening:
Hi everyone. I have a solution which seems to produce the correct outputs, but they are not being accepted as valid answers. I’ve tried to figure out why this could be, but I can’t come up with anything. Does anyone have any thoughts?

Thanks in advance.

Your code so far

var   cipherSet = ['N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M'];
var outputArray = [];
var outputString;     

function isLetter(str) 
{
  return (str.length === 1 && str.match(/^[A-Z]*$/));
}

function rot13(str) 
{// for non-alphabetic characters; do not transform them.  pass them along to outputArray
  
 // for every character in the input string, locate that character in alphabetSet
 // use the index value into alphabetSet on cipherSet to find the encoded value.
 // after the encoded value is found, put it into outputAray
  
  for(var i = 0; i < str.length; i++)
  {// for every character in the string ...
    if(isLetter(str[i]))
    {// if the character IS a letter...
      outputArray.push(cipherSet[alphabetSet.indexOf(str[i])]);// index where the letter in found  
    }
    else
    {// if the character is NOT a letter ...
      outputArray.push(str[i]);
    }
  }
  
  outputString = outputArray.toString().replace(/,/g, '');
  
  return outputString;

  
}
// Change the inputs below to test
rot13("GUR DHVPX OEBJA QBT WHZCRQ BIRE GUR YNML SBK.");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36.

Link to the challenge:


#2

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the new value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2

#3

Right, of course. Thank you! I feel so silly for that oversight.