Freecodecamp: Caesars Cipher: My code pass all the tests but doesn't validate on FCC

Created on 22 Jul 2016  路  23Comments  路  Source: freeCodeCamp/freeCodeCamp

Challenge Caesars Cipher has an issue.
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36.

The code pass all the tests but doesn't validate on FCC editor. Why that?

My code:

let engine = (v) => {

  let low = 'A'.charCodeAt();
  let high = 'Z'.charCodeAt();

  if (!!v.match(/[a-z]/i)) {
    if (v.charCodeAt() - 13 < low) {
      let code = (high - (low - (v.charCodeAt() - 13 + 1)));

      return String.fromCharCode(code);
    }
    else {
      return String.fromCharCode(v.charCodeAt() - 13);
    }
  }
  else {
    return v;
  }
};

function rot13(str) { // LBH QVQ VG!

  return '"' + str
    .split('')
    .map(engine)
    .join('') + '"';
}

// Change the inputs below to test
rot13("SERR PBQR PNZC");
rot13("SERR CVMMN!");
rot13("SERR YBIR?");
rot13("GUR DHVPX OEBJA QBT WHZCRQ BIRE GUR YNML SBK.");

help wanted

Most helpful comment

Yeah @eakl I got that too... I share your frustration.
Anyhow to contributors please update the messages to not have the quotes.

as in:

image

for all the tests.

All 23 comments

I am not an ES6 expert, requesting assistance from @FreeCodeCamp/issue-moderators
By far the output is matching the expected criteria.

The only thing that I can play a hunch is with the error that shows on the Dev Console:

image

Here is the complete log

Can anyone guide?

Could it be the global scope of 'engine'? Everytime a test runs it tries to define 'engine' again?

I had a quick search in the repo for 'engine' being defined anywhere, but could only find it in some express files as a string.

Nope already checked that, changing the name is not helping. Also I tried refactoring the code to test global declaration to be a problem.

If you check the complete log there is a weird error thrown by the RX.js library which I believe only @BerkeleyTrue has a good understanding of.

I can't run ES6 on my phone, I'll try it out when I get a chance

Sorry guys, but the code is incorrect. Running the code above with two minor changes allows it to pass all tests.

The original code returns a string containing an extra pair of double quotes, eg '"FREE PIZZA!"' instead of 'FREE PIZZA!', and deleting the pair of double quote concatenations in the return statement fixes that problem.

There does seem to be some conflict with the inner workings of the editor when using the variable name engine, but this doesn't stop the tests from passing successfully.

@BKinahan mind submitting your code in here? I could not seemingly pass it.

@raisedadead It really is the same code as above without the concatenations I mentioned. You may be having the issue where a page refresh is needed to allow test results to be updated, which affects many challenges in certain conditions.

Here's the code anyway for the sake of completeness:

let engine = (v) => {

  let low = 'A'.charCodeAt();
  let high = 'Z'.charCodeAt();

  if (!!v.match(/[a-z]/i)) {
    if (v.charCodeAt() - 13 < low) {
      let code = (high - (low - (v.charCodeAt() - 13 + 1)));

      return String.fromCharCode(code);
    }
    else {
      return String.fromCharCode(v.charCodeAt() - 13);
    }
  }
  else {
    return v;
  }
};

function rot13(str) { // LBH QVQ VG!

  return str
    .split('')
    .map(engine)
    .join('');
}

// Change the inputs below to test
//rot13("SERR PBQR PNZC");
rot13("SERR CVMMN!");
//rot13("SERR YBIR?");
//rot13("GUR DHVPX OEBJA QBT WHZCRQ BIRE GUR YNML SBK.");

Nope this doesn't let me pass the challenge.
I am on Windows 7 - Version 52.0.2743.82 m

Okay I did a full reset and cleared my cache then used the code you mentioned.
Passed with flying colors!

Thanks for the help @BKinahan

Yet I think the test messages are confusing, where a camper will definitely add the quotes as stated.
IMHO we should fix this.

@raisedadead My first attempt was without the quotes. It didn't work so I added the quotes without more success then posted an issue. Now I just removed the quotes and refreshed the page as mentioned by @BKinahan and it works. Weird...

@BKinahan Thanks! Sorry for having posted an issue for just a little quote :)

Nice catch @BKinahan , but that has raised two issues:

  1. Unclear instructions for the result expected
  2. codeUri not extracting and running the updated code without a refresh

Yeah @eakl I got that too... I share your frustration.
Anyhow to contributors please update the messages to not have the quotes.

as in:

image

for all the tests.

Personally, I can't recall ever having to refresh the browser to get some code to run apart from testing this issue.

Does it warrant a separate issue? Does it happen often enough?

@Bouncey It happens to me a lot, but it may be due to my use of ES6 features like arrow functions (which notably are used in this case). More testing needed 馃槃

@Bouncey It happened 3 times for me so far. Including this challenge.

@eakl to be honest we don't have 100% ES6 support for challenges yet. So it could be anything, to best of my knowledge Chrome has started supporting only in the latest of v52 I think. Many things will be broken.

I've only just started using ES6, and never in FCC, so I think it is an ES6 issue

@raisedadead @Bouncey I mostly use ES6 when I can (arrow function, const/let, argument operators, ...). It may be that indeed.

@raisedadead Does your note above imply that all other challenges whose functions return strings should also display them without quotation marks? E.g. Spinal Tap Case, Repeat a string repeat a string, and others..

I haven't seen this type of mistake in relation to this challenge or those others with similar outputs, so it may be quite uncommon. If so it might not justify a broad change, especially for test cases which should return empty strings and can't be represented without the quotes 馃槃

Yeah, that is correct.
But this is the behavior that you would see in the Dev Console.

So I think its not ambiguous.

image

But yeah representing empty string would be tough in the test messages.
Any interesting ideas are most welcome

solved this question using the charCode methods decided to solve it without those methods, solution seems okay but not working.

function rot13(str) { // LBH QVQ VG!
  str = str.toUpperCase().split("");
  var abc = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
  var indices = [];
  var newString = [];
  var rot13arr = [];
  var newStringrot13arr = [];
  function getindices(){
    abc.forEach(function(val,index){
      if(index + 13 > 25){
        indices.push(index -13);
      }
      else{ 
        indices.push(index + 13);
      }
    });
  }
  function newStringFunc(){
    str.forEach(function(val){
      if(abc.indexOf(val) == -1){
        newString.push(val);
      }
       else{newString.push(abc.indexOf(val));}
    });
  }
  function rot13array(){
    newString.forEach(function(val){
      if(indices.indexOf(val) == -1){
        rot13arr.push(val);
      }
      else{ rot13arr.push(indices.indexOf(val));}
    });
  }
  function newRot13str(){
    rot13arr.forEach(function(val){
      if(abc[val] === null ||abc[val] === undefined || abc[val] === ""){
        newStringrot13arr.push(val);
      }
      else{newStringrot13arr.push(abc[val]);}
    });
  }
  getindices();
  newStringFunc();
  rot13array();
  newRot13str();
  //var finalString  = newStringrot13arr.join(" ");
  //return '"' + finalString + '"';
  return newStringrot13arr.join(" ");
}


// Change the inputs below to test
rot13("SERR PBQR PNZC");

help needed.


Fixed markdown code format by @abhisekp

@iykyvic, this place is not for get a help.
Please use the Help chat room for getting challenge related help.

Happy Coding!

@texas2010, thank you for responding, I already solved the problem using the charCode methods, so I don't need help solving this one, I am just asking why this code(previous comment) which works does not pass the test.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ar5had picture ar5had  路  3Comments

imhuyqn picture imhuyqn  路  3Comments

robwelan picture robwelan  路  3Comments

DaphnisM picture DaphnisM  路  3Comments

vaibsharma picture vaibsharma  路  3Comments