DEV Community

Discussion on: Daily Challenge #42 - Caesar Cipher

Collapse
 
alvaromontoro profile image
Alvaro Montoro

JavaScript

const cipher = (s, k) => s.toLowerCase()
                          .split('')
                          .map(c => c.charCodeAt(0) < 'a'.charCodeAt(0) || c.charCodeAt(0) > 'z'.charCodeAt(0) 
                                    ? c
                                    : c.charCodeAt(0) > 122 - k
                                      ? String.fromCharCode(97 + (c.charCodeAt(0) + k - 1) % 122)
                                      : String.fromCharCode(c.charCodeAt(0) + k))
                          .join('');

I imagine that for now, the only way to decypher a phrase would be to check each word to verify that it is valid using either a dictionary or an API. Which may be a pain.

A thing that is not going to work now, but maybe it could work in the future (big "maybe" here, but bear with me for a second):

  • Browsers contain their own dictionary for spell checking.
  • CSS has pseudo-elements for grammar and spelling errors that allows to target content within an element that the browser has identified as having grammar mistakes (::grammar-error) or being misspelled (::spelling-error).
  • We could create a hidden input, add the content of the decyphered text and check if there is a pseudo-element that fulfills one of the error pseudo-elements.

It would be super hacky, but it would allow us to use the browser dictionary (if it's not open already, in which case, we should use it) instead of having our own dictionary or having to call an API.

Unfortunately, ::grammar-error and ::spelling-error are not currently supported by any browser, so I cannot test this crazy idea at the moment.