I am working on a Codewars problem and have been able to solve the majority of this problem except the final part. The challenge is "rot13"
ROT13 is a simple letter substitution cipher that replaces a letter with the letter 13 letters after it in the alphabet. ROT13 is an example of the Caesar cipher. Create a function that takes a string and returns the string ciphered with Rot13.
function rot13(message){
message = message.split('');
let alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
let indexes = message.map(char => alphabet.indexOf(char));
let result = indexes.map(i => {
let val = i + 13;
let max = 26;
if (val > max) {
let temp = val - max;
val = temp;
}
return val;
});
//result = [6, 17, 5, 6];
//i want to use the elements in my result array, and
//grab the letters from my alphabet array whose indexes associate with those elements from my result array
}
rot13('test') // 'grfg'
This is my current state in this problem. I have tried checking if any of the indexes of the elements in alphabet === the elements in my result array and if so, pushing those characters from the my alphabet array into an empty array but I am receiving -1
Any suggestions for approaching this problem/altering my thought process will be helpful. Thanks!
result.map(i => alphabet[i])map, do it in the one you already have:return val;->return alphabet[val];You do need toreturn result;at the end of your function, though