-1

I think I am close to figuring this out.

Object is to take a string of "encrypted" characters and decipher them to actual words. Perhaps my code isn't very elegant right now, I'll get there, but I CAN get the beginning 'str = "SERR PBQR PNZC"' to return the correct ASCII code using charCodeAt(), add or subtract 13 as needed, and concat that into strArray--I know, it's not an array. But I can't get strArray to become an array, so I can pass it to String.fromCharCode(null, strArray); to have it return the correct deciphered text.

My code is below:

function rot13(str) { // LBH QVQ VG!

var strArray = '';

//var right = [70, 82, 69, 69, 32, 67, 79, 68, 69, 32, 67, 65, 77, 80];
//used above variable with String.fromCharCode.apply(null, right); and it worked.

for(var i = 0; i < str.length; i++){
 //console.log(str.charCodeAt(i));
  if(str.charCodeAt(i) >= 65 && str.charCodeAt(i) <= 77) {
    strArray = strArray.concat(str.charCodeAt(i) + 13, ' ');
  } else if(str.charCodeAt(i) >= 78) {
    strArray = strArray.concat(str.charCodeAt(i) - 13, ' ');
  } else {
    strArray = strArray.concat(str.charCodeAt(i), ' ');
  }
 }
 return strArray;
}

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

Any help will be greatly appreciated.

Rsp8
  • 133
  • 1
  • 10
  • I did try to use stryArray.split(' '); but that didn't work. – Rsp8 Nov 09 '17 at 02:51
  • What is your current problem? – zerkms Nov 09 '17 at 02:51
  • Push all the numbers in an array, then turn that array into a string using another loop. – zerkms Nov 09 '17 at 02:55
  • @zerkms keep in mind strings are a character array. – stealththeninja Nov 09 '17 at 03:00
  • 1
    You're right -- you are close. The piece you seem to be missing is converting the numbers back into letters. You are making a string of individual numbers, but you probably want to use `String.fromCharCode()` to turn them back to letters. – Mark Nov 09 '17 at 03:03
  • @stealththeninja they don't have characters, they have numbers. – zerkms Nov 09 '17 at 03:41
  • @stealththeninja you're right, I have successfully used String.fromCharCode() to convert the ASCII code back into a letters, however, I can't complete the challenge (freeCodeCamp), because there is a space being converted at the end. – Rsp8 Nov 09 '17 at 13:32
  • My code below is returning the correct "deciphered" string, however, with a space at the end so it is not completing the challenge: – Rsp8 Nov 09 '17 at 13:35

1 Answers1

0

Ok, so, below returns the correct string, save for the space at the end; which doesn't allow me to complete the challenge.

function rot13(str) { // LBH QVQ VG!

  var strArray = '';

  for(var i = 0; i < str.length; i++){
    //console.log(str.charCodeAt(i));
    if(str.charCodeAt(i) >= 65 && str.charCodeAt(i) <= 77) {
      strArray = strArray.concat(str.charCodeAt(i) + 13, ' ');
    } else if(str.charCodeAt(i) >= 78) {
      strArray = strArray.concat(str.charCodeAt(i) - 13, ' ');
    } else {
      strArray = strArray.concat(str.charCodeAt(i), ' ');
    }

  }
  var correct = strArray.split(' ');
  console.log(correct);
  return String.fromCharCode.apply(null, correct);
}

// Change the inputs below to test
rot13("SERR PBQR PNZC");
Rsp8
  • 133
  • 1
  • 10