Caesars Cipher Problem

Tell us what’s happening:

Your code so far

function rot13(str) { 
  var arr = str.split(" ");
  var newArr = [];
  var temp = 0;
  for(var i = 0; i < arr.length; i++){
	  for(var j = 0; j < arr[i].length; j++){	
		if(arr[i].charCodeAt(j) >= 78 && arr[i].charCodeAt(j) <= 90){
			 temp = String.fromCharCode( arr[i].charCodeAt(j) - 13);
			newArr.push(temp);
		}
		else if(arr[i].charCodeAt(j) < 78 && arr[i].charCodeAt(j) >= 65){
			  temp = String.fromCharCode( arr[i].charCodeAt(j) + 13);
			newArr.push(temp);
		}
		else{
			  temp = String.fromCharCode( arr[i].charCodeAt(j));
			newArr.push(temp);
		}
	  }
	 newArr.push(" ");
  }
  var newStr = newArr.join("");
  return newStr;
}
rot13("SERR PBQR PNZC");
rot13("SERR CVMMN!");

I don’t understand why is this code is not passing me to the next chalenge. It’s working. It returns a decoded string. Can anyone please explain me what’s wrong?

The strings that you return have a trailing space on them. See this:

You can get rid of trailing spaces using .trim().

1 Like

Thank you very much! It was so easy, but I let it out of my sight :slight_smile: Now, I understand