Quarantine sucks. I was bored so I wrote a code generator for my kids so that I could leave them secret codes around the house.

Here's the code for a simple cipher (which is about all my 4 & 6 year old can handle). It's turned out to be good practice for the alphabet recognition in the 4 year old and reading/problem solving for the older one.

here's the code...


const secretMessage = "Laborum minim veniam ex culpa laboris Lorem Lorem. Dolor ut mollit eiusmod minim fugiat nulla proident. Amet enim reprehenderit eu non ullamco minim nisi sit ex Lorem quis. Dolor nostrud in cupidatat aute aliqua voluptate velit est irure."

const 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'];

const generateOffsetAlphabet = () => {
    const indexOffset = Math.round(Math.random() * 25)
    const newAlpha = new Array(25);
    alphabet.forEach((char, index) => {
        newIndex = (index + indexOffset) % 25
        newAlpha[newIndex] = char;
    })
    return newAlpha;
}

const encodeMessage = (message) => {
    const normalizedMessage = message.toLowerCase();
    const secretAlphabet = generateOffsetAlphabet();
    let encodedMessage = '';
    for( let i = 0; i < normalizedMessage.length; i++) {
        const targetChar = normalizedMessage.charAt(i);
        const encodedChar = alphabet.indexOf(targetChar) >= 0 ? secretAlphabet[alphabet.indexOf(targetChar)] : targetChar
        encodedMessage = `${encodedMessage}${encodedChar}`;
    }
    return encodedMessage;
}

const secretCode = encodeMessage(secretMessage)

console.info(secretCode)

Enjoy!