Mixed Messages - Web UI - Past Lives

Hello there! After a little time off (family stuff), I’m back on track with the full-stack course. I’ve finished the Mixed Messages project and decided to make a web UI with the “Past lives” theme.

It’s pretty simple, it just combines data from four different arrays so you can have more than 6 millions possible combinations (considering personal traits). Hope you like it :smiley:

Live Version
Repository

Here is the script for the node.js app (you can use it with the same JSON that’s in the repo).

// Import required modules
const fs = require('fs').promises;

// Function to get a random item from an array
function getRandomItem(array) {
    return array[Math.floor(Math.random() * array.length)];
}

// Function to generate the past life sentence
async function generatePastLife() {
    try {
        const data = JSON.parse(await fs.readFile('./data.json', 'utf8')); // Function to load data from the JSON file

        const characteristic = getRandomItem(data.characteristics).toLowerCase();
        const profession = getRandomItem(data.professions).toLowerCase();
        const action = getRandomItem(data.actions).toLowerCase();
        const cause = getRandomItem(data.causes).toLowerCase();

        const pastLife = `You were a ${characteristic} ${profession} that ${action}, because you ${cause}.`;
        console.log(pastLife);
    } catch (error) {
        console.error('Error generating past life:', error.message);
    }
}

// Main function to execute the app
async function main() {
    console.log('Welcome to the Past Life Generator!');
    console.log('Discover who you were in a past life...\n');

    // Generate a past life
    await generatePastLife();
}

main();

See ya!