# Caesars Cipher Help

**URL:** <https://forum.freecodecamp.org/t/caesars-cipher-help/51699>\
**Category:** General\
**Created:** [November 4, 2016, 3:53am UTC](https://forum.freecodecamp.org/t/caesars-cipher-help/51699 "2016-11-04T03:53:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MitchWilkins](https://avatars.discourse-cdn.com/v4/letter/m/aeb1de/32.png) [@MitchWilkins](https://forum.freecodecamp.org/u/MitchWilkins)\
**Post date:** [November 4, 2016, 3:53am UTC](https://forum.freecodecamp.org/t/caesars-cipher-help/51699/1 "2016-11-04T03:53:34Z")

</div>

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:

```auto
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");

```

---

<div class="post-metadata">

**Author:** ![JacksonBates](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jacksonbates/32/123402_2.png) [@JacksonBates](https://forum.freecodecamp.org/u/JacksonBates)\
**Post date:** [November 4, 2016, 5:34am UTC](https://forum.freecodecamp.org/t/caesars-cipher-help/51699/2 "2016-11-04T05:34:11Z")

</div>

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:

```javascript
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 🙂

---

<div class="post-metadata">

**Author:** ![MitchWilkins](https://avatars.discourse-cdn.com/v4/letter/m/aeb1de/32.png) [@MitchWilkins](https://forum.freecodecamp.org/u/MitchWilkins)\
**Post date:** [November 4, 2016, 6:41am UTC](https://forum.freecodecamp.org/t/caesars-cipher-help/51699/3 "2016-11-04T06:41:39Z")

</div>

@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.

```auto
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?");

```

---

<div class="post-metadata">

**Author:** ![MitchWilkins](https://avatars.discourse-cdn.com/v4/letter/m/aeb1de/32.png) [@MitchWilkins](https://forum.freecodecamp.org/u/MitchWilkins)\
**Post date:** [November 4, 2016, 7:00am UTC](https://forum.freecodecamp.org/t/caesars-cipher-help/51699/4 "2016-11-04T07:00:22Z")

</div>

I ended up changing

````auto
      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.
````

---

<div class="post-metadata">

**Author:** ![JacksonBates](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jacksonbates/32/123402_2.png) [@JacksonBates](https://forum.freecodecamp.org/u/JacksonBates)\
**Post date:** [November 4, 2016, 7:10am UTC](https://forum.freecodecamp.org/t/caesars-cipher-help/51699/5 "2016-11-04T07:10:00Z")

</div>

Good job!

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

---

<div class="post-metadata">

**Author:** ![jwilkins.oboe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jwilkins.oboe/32/179497_2.png) [@jwilkins.oboe](https://forum.freecodecamp.org/u/jwilkins.oboe)\
**Post date:** [January 16, 2021, 5:31am UTC](https://forum.freecodecamp.org/t/caesars-cipher-help/51699/6 "2021-01-16T05:31:17Z")

</div>

Closed on January 16, 2021, 5:31am UTC


