Caesars cipher challenge. help

Caesars cipher challenge. help
0

#1

hi I am looking for some help with this code. I have a challenge with the fromCharCode(). I am try to make the array as an argument.

function rot13(str) { // LBH QVQ VG!
  
  str = str.replace(/[^A-Z]/g, '');
  console.log(str);
  
  var arr = str.split('');
  var array = [];
  for(var i = 0; i < arr.length; i++){
    array.push(arr[i].charCodeAt());
  }
  
  var Strings;
  
  Strings.fromCharCode.apply(null, array);
   
  console.log(string);
  
  return Strings;
}

#2

Can you try rephrasing your question into something specific? What do you not understand? Is fromCharCode not doing what you expect?


#3

thanks @frenata.

My question is how can I pass an array to the function fromCharCode().

right now the variable array is an array of numbers, but I want to convert those numbers into an argument that I can use the function fromCharCode().

i am working on the solution for challenge ceasars cipher;


#4

I’ve edited your post for readability. When you enter a code block into the forum, 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.


#5

To answer your question, if I understand you correctly, you are trying to find a way to feed an array of char codes (numbers) to the String.fromCharCode.

Of course, that doesn’t take an array, but a list of numbers. One easy way to deconstruct an array into a list of it’s elements it with the spread operator, which is three periods, ...

var charArr = [72, 111, 119, 100, 121, 33]

var newStr = String.fromCharCode(...charArr)

console.log("The original array =", charArr)
console.log("The new string =", newStr)

String.fromCharCode(...charArr) is the same as String.fromCharCode(72, 111, 119, 100, 121, 33)


#6

First place to look for these things is the docs. MDN is a great source for such things, here’s the doc on fromCharCode.

Next, what I’d recommend is you start playing with the function in a console somewhere. What happens when you give it a single number? Several numbers? An array of numbers?

I think that’s enough information to get you further along the path.

To correct a small error/misunderstanding in your existing code:

var Strings; 

Strings.fromCharCode ....

var Strings; is unnecessary and actively will cause your program to not behave as expected. fromCharCode is a static method, which means you don’t need to call it on a variable at all, just on the static object String (not Strings) which already exists.

I’m also unsure what you’re attempting to do with apply. Can you explain?


#7

I think the ultimate goal was something like:

return String.fromCharCode.apply(null, array);

but since array did not have the correct ascii codes (based on incorrect logic in code above), this line would still not pass the challenge tests. If array would have contained the correct codes, then this line would have returned the correct answer.


#8

Thanks.

it helped. why are the three dots showing a warning?


#9

@RandellDawson is right that’s what I was trying to achieve.


#10

could you please explain your comment regarding the array containing the correct code?


#11

It’s an ES6 feature I think? So the built in linter will gripe at you unless you specify in a special comment that you’re using those features. You can also just safely ignore it.


#12

What part of my comment did you not understand?


#13

this is the part I didn’t understand. What do you mean by the correct codes?