Caesars Cipher - fromCharCode issues

Tell us what’s happening:
When I console.log newstring. I get the correct values for free code camp.

70,82,69,69,32,67,79,68,69,32,67,65,77,80

When I try to feed this to fromCharCode it doesn’t seem to function correctly:
String.fromCharCode(newstring)

Any idea why the fromCharCode isn’t working as expected?

Thanks again!

Your code so far

function rot13(str) { // LBH QVQ VG!
  var newstring ="";
  
  for (var index =0; index < str.length; index++){
  if (str.charCodeAt(index) >= 78){
    newstring += str.charCodeAt(index) - 13 + ",";
   }
 
 
  if ((str.charCodeAt(index) < 78) && (str.charCodeAt(index) !=32)){
    var difference = 0;
    var from13 = 0;
    difference = str.charCodeAt(index) - 64;
    from13 = 13 - difference;
    newstring += 90 - from13 + ",";
     }
    
    if(str.charCodeAt(index)== 32){
      newstring += 32 + ",";
    }
    
  }
   
  var translated = String.fromCharCode(newstring);
   console.log(translated);
  return str;
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36.

Link to the challenge:

When you write:

var translated = String.fromCharCode(newstring);
console.log(translated);

you have to remember that newstring is a string of number characters separated by commas. String.fromCharCode expects a number to be passed to it. You are passing a string and not a number.

1 Like

Thanks. I thought I could just pass that into fromCharCode (maybe auto conversion?). Any hints on how to convert it? It seems to accept comma separated numbers.

Thanks again. That’s what I ended up doing and then converting it to a number with array.map(Number). It worked.