Caesars Cipher Help

Caesars Cipher Help
0

#1

My logic is that I want to take the numbers in an array, change them to the correct numbers (to match the unicode), then convert the correct number to the letters, and reattach as a string.

I keep getting a bunch of “\u0000” populated in my Array. Any suggestions?
Thanks,
Mitch

So far this is my code:

function rot13(str) { // LBH QVQ VG!
  var letters = "";
  var convert = "";
  function numberConvert(number) {
    var abs = 0;
    if (number === 32 ) {
      return 32;
      
    } else if (number > 77) {
      return number - 90 + 65;
    } else {
      return number;
    }
  }
  var newArray = [];
  var split = str.split('');
  var ccode = "";
  for (var i = 0; i < split.length; i++) { 
  ccode = split[i].charCodeAt(0);
    newArray.push(ccode);
  }
  for (var j = 0; j < split.length; j++) { 
    newArray.unshift(String.fromCharCode(split[j]));
    newArray.pop(ccode);

  }
  return newArray;
}

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

#2

You have several issues in here, but I’ll point out some of the bigger ones to get you back on track.

In the second for loop you have this line:

newArray.unshift(String.fromCharCode(split[j]));

Think carefully about what newArray and split contain so you can see what you are asking this loop to do.

Also, review what unshift and pop do. These don’t behave the way you think they do. In particular, pop doesn’t take any arguments, so passing ccode to it doesn’t do anything.

You have created a convert function, but you don’t actually use it yet - I think you probably already know that and you’re just trying to get the other bit working first, but I thought I should mention it just in case.

You are on the right track though, so keep plugging away :slight_smile:


#3

@JacksonBates Thanks a ton! Almost all of them work except for the one with a question mark.
The question mark always returns and L. I’ve messed around with the code a lot and I can’t find the source of it.
What do you think? I think It has something to do with the conversion, but not sure.

function rot13(str) { // LBH QVQ VG!
  var letters = "";
  var convert = "";
  function numberConvert(number) {
    if (number -13 === 32) {
      return 32;
    } else if (number - 13 > 77) {
      return number - 90 + 64;
    } else if ( number >= 65){
      return number;
    }else {
      return number - 13;
    }
  }
  var newArray = [];
  var split = str.split('');
  var ccode = "";
  for (var i = 0; i < split.length; i++) { 
  ccode = split[i].charCodeAt(0);
    newArray.push((numberConvert(ccode + 13)));
  }
  for (var j = 0; j < split.length; j++) { 
  newArray.unshift(String.fromCharCode(newArray[j+j]));

  }
  
  letters = newArray.slice(0,str.length).reverse();
  var reverse = letters.join('');
  //return reverse;
  return reverse;
}

// Change the inputs below to test
rot13("SERR YBIR?");

#4

I ended up changing

      return 32;```

to

```    if (number -13 === 63) {
      return 63;```
and it **worked!**
I know there are more efficient solutions out there. I just need more practice haha.

#5

Good job!

Don’t worry too much about efficiency yet - that’ll come with a lot more experience :slight_smile: