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: