Caesars Cipher can't understand why undefined

Caesars Cipher can't understand why undefined
0.0 0

#1

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;
   //console.log(newStr.charCodeAt(newStr.length-1));
  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;  
      //console.log(finalStr);
    } else finalStr = addedStr - x;
     newStrArr.push(finalStr);
      
  } //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:


#2

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.


#3

oops, thanks, Randell!


#4

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!