Hi!, i wanna share mi code so someone can tell me whats wrong or what can i change, because everything is good except when i console log result , its something like this:
[ ‘f’ ]
[ ‘r’ ]
[ ‘e’ ]
[ ‘e’ ]
[ ‘c’ ]
[ ‘o’ ]
[ ‘d’ ]
[ ‘e’ ]
[ ‘c’ ]
[ ‘a’ ]
[ ‘m’ ]
[ ‘p’ ]
function rot13(str) {
var abecedary = "abcdefghijklmnopqrstuvwxyz";
var rot13 = "NOPQRSTUVWXYZABCDEFGHIJKLM"
var finalString = str.replace(/\s/g, "")
for (let l = 0; l < str.length;l++){
let findCharacter = rot13.indexOf(finalString[l])
var result = abecedary.charAt(findCharacter).split("")
}
console.log(result)
}
rot13("SERR PBQR PNZC");
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
Hi gdennci , thanks for the answer!
The problem I have is that I want to concatenate the letters that remain one below the other, and I don’t know how to do it
So charAt is giving you back a string, yes? And you want result to be the final confined string? Can you think of something you’ve learned to add a string onto another string, rather than adding to an array with split()?
I see. So the thing is that split does not help you with that. As the name already suggests, it splits a String on a specific character that you provide. E.g. "foo:bar".split(":") returns an array with the values ["foo", "bar"].
What you want is to concatenate the characters that you found into a new string. Concatenating is just another word for attaching something, in your case to a string.
I’ll give you a hint how you can do that:
(1) Create an empty string e.g. my_result = ""
(2) Concatenate the characters that you found inside the for loop to that String variable. You can use the + operator like so my_result = my_result + "a", or the short version my_result += "a"