# Caesars Cipher Help Help

Caesars Cipher Help Help
0
#1

I am about halfway through with the Casesar’s Cipher puzzle. The Algorithm is incomplete, so disregard the last line of code. What I am wondering is why my

``````for (var i = 0; i < newArray.length; i++) {

unicodeArray.push(newArray[i].charCodeAt());
}

``````

block of code works (I understand I am somehow using a string method on an array)
but this next block of code doesn’t work although very similar.

``````for (var x = 0; x < minus13Array.length; x++) {
newLetterArray.push(minus13Array[x].fromCharCode());
}
``````

I think I can figure the rest of it out if I can figure this one bit out.

``````function rot13(str) { // LBH QVQ VG!
var newArray = str.split("");
var newString = "";
var unicodeArray = [];
var minus13Array = [];
var newLetterArray = [];
for (var i = 0; i < newArray.length; i++) {

unicodeArray.push(newArray[i].charCodeAt());
}
for (var j = 0; j < unicodeArray.length;j++) {
minus13Array.push((unicodeArray[j] - 13));
}
for (var x = 0; x < minus13Array.length; x++) {
newLetterArray.push(minus13Array[x].fromCharCode());
}

return newLetterArray;

}

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

Your Browser User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:54.0) Gecko/20100101 Firefox/54.0.1 Waterfox/54.0.1`.

0 Likes

#2

I can see why you might be confused. If you read the fromCharCode documentation under the Description section, you see that it must always use it in the following way:

``````var numericCode = 65 // ASCII code value for the capital letter 'A'
String.fromCharCode(numericCode) // results in 'A'
``````

The word “String” used above is not arbitrary.’’

However, in the charCodeAt, you can supply your own string like:

``````var myString = 'JavaScript';
myString.charCodeAt();
// the above results in 'J' because if you do not supply an index value for argument,
// the default 0 is used for the index and myString[0] is 'J'
``````
0 Likes

#3

Ahh. I think I understand. So this line of code

``````unicodeArray.push(newArray[i].charCodeAt());
``````

only worked because by default without an argument, the method indexes at [0], and since [i] iterates only one character at a time, it’s length at all times could be said to be 1? If that is the case I see now why that worked and the second one did not.

0 Likes

#4

Now that you have learned something, what will you do to fix the error you are getting in your code below?

``````newLetterArray.push(minus13Array[x].fromCharCode());
``````
0 Likes

#5

One more thing about using charCodeAt(). The following code is for demonstration purposes only.

``````var myString = 'JavaScript';
for (var i = 0; i < myString.length; i++) {
console.log(myString[i] + ' has code value of ' + myString.charCodeAt(i));
}
``````

Running the above code results in:

J has code value of 74
a has code value of 97
v has code value of 118
a has code value of 97
S has code value of 83
c has code value of 99
r has code value of 114
i has code value of 105
p has code value of 112
t has code value of 116

0 Likes

#6

This is what I have now. I know I’m doing something really dumb.

``````for (var x = 0; x < minus13Array.length; x++) {
newLetterArray.push(minus13Array[x].charCodeAt(minus13Array));
}
``````

Trying to push into a new array. Since charCodeAt() needs indexes as arguments to run, I figured the fully popuated minus13Array would work. Keeps telling me that ‘TypeError: minus13Array[x].charCodeAt is not a function’.

0 Likes

#7

Put the following console.log statement inside the for loop to see what minus13Array[x] is in the first iteration of the loop before the error appears. I think you will be surprised.

``````console.log(minus13Array[x]);
``````

Also the argument that charCodeAt takes should be an index (integer). You are supplying it with minus13Array which is an array.

0 Likes

#8

I added that line in there, but it’s still saying that my code is not a function. I thought it would run the console code first because it came before it. here’s what I have: (also, i decided to try giving it integer arguments, an arbitrary index and length but that produced no change.)

``````for (var x = 0; x < minus13Array.length; x++) {

console.log(minus13Array[x]);
newLetterArray.push(minus13Array.fromCharCode(4,6));
}``````
0 Likes

#9

If I run your code with the console.log statement I mentioned, I get:

``````70
TypeError: minus13Array[x].charCodeAt is not a function
``````

The 70 is the first element in the minus13Array array. 70 is a number and can not use charCodeAt, because it is a String prototype.

0 Likes