Caesars Cipher ("-" between spaces)

Caesars Cipher ("-" between spaces)
0

#1

Tell us what’s happening:
ok i think i have almost solved the problem…
but there is a little bug…which is “-” appearing on the blank strings…as you can see it in the output.
so how can i remove it and have “space” instead.

Your code so far

function rot13(str) { // LBH QVQ VG!
  
  var arr = [];
  var fixed = [];
  var final = [];
  var result = "";
  
  for(i = 0; i<str.length; i++){
    arr.push(str.charCodeAt(i));
  }
  
arr.map(function(val){
    fixed.push(val + 13);
 });

  for (j = 0; j<fixed.length; j++){
    if (fixed[j] > 90){
      final.push((fixed[j]-90)+64);
    }
    else{
      final.push(fixed[j]);
    }
  }

result += String.fromCharCode.apply(null, final);

  return result;
}

//OUTPUT--> FREE-CODE-CAMP

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36.

Link to the challenge:


#2

How can you change your logic so that it only changes the values of characters that are letters?


#3

i was thinking about filter() …but not sure how to use it


#4

filter is generally going to be used to remove values from the array. What you want is for the character codes for non-letters to go into your fixed array without being changed (without adding 13) or for those to be changed back to their original values in your for loop (less efficient of course, but perhaps easier for you since you already have some logic like this).


#5

not happening … a little help here …perhaps


#6

Well, what have you tried since changing your code to the above? What worked? What didn’t? What behaved differently than expected?


#7

Going back to what @ArielLeslie asked you (see below). You are currently adding 13 to the space character’s ascii code (20), so you end up up pushing the value 45 to fixed and 45 is the hyphen character. What about adding an if statement to check for ascii values between a certain range (upper A through upper Z) and only push val + 13 if the if statement evaluates to true?