Skip to content
This repository was archived by the owner on Mar 16, 2026. It is now read-only.
This repository was archived by the owner on Mar 16, 2026. It is now read-only.

A shift cipher takes a plain text message and shifts each letter forward in the alphabet by a given number. #1045

@oldoc63

Description

@oldoc63

For example, a shift cipher with a shift of 1 would turn the string 'hello' to 'ifmmp'.
Create a class ShiftCipher that takes the numerical value of the shift as a constructor parameter. The class should have to methods:

  • encrypt: takes a plain text string and returns a capitalized string with each letter shifted forward in the alphabet based on the set shift value.
  • decrypt: takes and encrypted message and returns a lower case string with each letter shifted back in the alphabet based on the set shift value.
  • In both methods, any character outside the alphabet should remain the same.
  • But if a character is shifted outside the alphabet in either direction it should be wrapped around to the other side. For example, encripting a y with a shift of 4 results in C and decrypting an A with a shift of 1 result in z.
  • Example:
const cipher = new ShiftCipher(2);
cipher.encrypt('I love to code!'); // returns 'K NQXG VQ EQFG!'
cipher.decrypt('K <3 OA RWRRA'); // returns 'i <3 my puppy'

Feel free to reference the Unicode Table as well as the JavaScript String methods including: toUpperCase(), toLowerCase(), charCodeAt() and fromCharCode()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions