Caesars Cipher dash between each word in output string

*Tell us what’s happening:
My output string has a dash between the words. The unicode for the space between the words are being counted as well?Would I have to use the replace method to get rid of the dash?

Your code so far

function rot13(str) { // LBH QVQ VG!
  var charNum = [];
  var numHolder = [];
  var asciiValues = [];
  str = str.toUpperCase();//changes the string to upper case so the ascii code will work with the same value.
  for( i = 0; i < str.length; i++)// for loop goes through the string and checks the ascii value. If the ascii value is greater than or equal to 78 it will subtract 13, else will add 13.
  {
   charNum.push(str.charCodeAt(i));//pushing the values into the charNum array.
     if(charNum[i] >= 78)// condition statement to check if the value is greater than or equal to 78. if so subtract 13
       {
         charNum[i] -= 13;
       }
      else// if not then add 13
        {
          charNum[i] += 13;
        }   
    asciiValues.push(String.fromCharCode(charNum[i]));//pushing the new ascii values into a new array
  }
  
 return asciiValues.join("");// prints out FREE-CODE-CAMP, how do I ignore the space value? because the space value is 32, which would add 13 making it 45 and unicode 45 is the dash.

}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:56.0) Gecko/20100101 Firefox/56.0.

Link to the challenge:

I figured the problem out. I changed my else statement to an if else and set it to greater than or equal to. This code was hard. I learned a lot.

function rot13(str) { // LBH QVQ VG!
  var charNum = [];
  var numHolder = [];
  var asciiValues = [];
  //str = str.toUpperCase();//changes the string to upper case so the ascii code will work with the same value.
  for( i = 0; i < str.length; i++)// for loop goes through the string and checks the ascii value. If the ascii value is greater than or equal to 78 it will subtract 13, else will add 13.
  {
   charNum.push(str.charCodeAt(i));//pushing the values into the charNum array.
   
     if(charNum[i] >= 78)//math stuff
       {
         charNum[i] -= 13;
       }
      _**else if( charNum[i] >= 65)**_
_**        {**_
_**          charNum[i] += 13;**_
        }   
    asciiValues.push(String.fromCharCode(charNum[i]));//pushing the new ascii values into a new array
  }
  
 return asciiValues.join("");// prints out FREE-CODE-CAMP, don't understand why there is a dash between my words.

}

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

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

markdown_Forums

1 Like