Random Zen Koan Generator

A simple script to generate a random ‘Zen Koan’ phrase.

Did not add anything above guidelines, returns random phrase when function is called.
Initially I wanted to create a document scraper function to pull structural words from an external document and format them into arrays, so that the random message was less “pre-determined” but decided to save something like that for when I have more knowledge.

REPO

Feel free to give input or ideas for modifications in the future.

Thanks!

I’m amused that random phrases can be attributed to random Zen masters.

Given what you have:

const returnZenFirst = () => {
    return zenFirst[Math.floor(Math.random() *9)];
};
const returnZenMiddle = () => {
    return zenMiddle[Math.floor(Math.random() *9)];
};

I don’t like the magic number 9 here. It’s also prone to failure if your array is a different length than you’ve statically assigned. It would be better to ask the array it’s length.

If you have a helper function like:

const randPick = xs => xs[Math.floor(Math.random() * xs.length)];

Then the rest of your functions that use such logic can leverage it:

const returnZenFirst = () => randPick(zenFirst);
const returnZenMiddle = () => randPick(zenMiddle);

If you find yourself writing the same thing more than once, consider a function that might shorten that process and centralize the logic. Then, if there’s an error in that logic, you need only fix it in one place.

2 Likes