Caesars Cipher can't understand why undefined

Caesars Cipher can't understand why undefined
0.0 0


Tell us what’s happening:
I’ve understood, that my code is not very beautiful, but can’t understand why the “undefined” is printed before my result? Could anybody help me?

Your code so far

function rot13(str) { // LBH QVQ VG!
  var newStrArr = []; 
  var x = 13;
  var addedStr;
  var finalStr; 
  var result;
  var final;
  for (var i = 0; i <str.length; i++){
       addedStr = str.charCodeAt(i);
     // console.log(addedStr);
    if (addedStr < 65 || addedStr > 90 ){
      finalStr = addedStr;
     // console.log(finalStr);
    else if (addedStr < 65+x){
      finalStr = addedStr + x;  
    } else finalStr = addedStr - x;
  } //console.log(newStrArr); 
  for(var j= 0; j <newStrArr.length; j++){
    result = String.fromCharCode(newStrArr[j]);
    final +=result;
}  console.log(final);
  //result = String.fromCharCode(result);
 // console.log(parseFloat(+result));
  return  final;

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

Your browser information:

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

Link to the challenge:


You declared final but did not initialize it to a specific value, so the first time the following line executes:

final +=result;

final is undefined, so undefined is concatenated onto the front.


oops, thanks, Randell!


It’s was very useful, that you just show me my mistake and don’t advise any solutions. I’ve solved the problem by myself!!! Thank you very much!