0

Hello I'm trying to solve the classic rotational cypher problem but im also trying to solve for when numbers are included in the string. So far this is the method I have been going about it using javascript:

function rotationalCipher(input, rotationFactor) {

const originalAlpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

return input.replace(/[a-z]/gi, letter => originalAlpha[originalAlpha.indexOf(letter)+rotationFactor]);
  

}

It works but it doesn't address the numbers within the string. Id be appreciative of any help.

  • If implemented correctly you should be able to simply add them to the `originalAlpha` string, however, I would expect some modulus calculation when calculating the new index. Why is the `i` flag there in your regex? – Maarten Bodewes Feb 21 '22 at 22:13
  • It was from some documentation that I copied I honestly don't know why. Also would you have an example by chance? – Luckthedev Feb 23 '22 at 02:54
  • There are many many examples of Caesar ciphers. Better try it yourself. Create a function that calculates the index in the alphabet, then do the shift by using `+` and a modular operation using the size of the alphabet at the right side. Finally, convert back to a character code using the shifted index. Good luck! – Maarten Bodewes Feb 23 '22 at 02:58

0 Answers0