Jump to content

Recommended Posts

Posted

Objective:
Create a program that generates custom holiday messages by combining templates and user input. The program should also allow the user to shuffle the words for fun.


Requirements:

  1. The program should provide a set of predefined holiday message templates, such as:

    • "Happy {holiday}, {name}! May your {wish} come true this season!"
    • "Wishing you a {adjective} {holiday}, {name}. Stay {wish} and joyful!"
  2. The program must:

    • Ask the user for inputs like the holiday name, their name, an adjective, and a wish.
    • Fill the templates with the user-provided data.
    • Display the final messages.
  3. Add a "word shuffle" feature:

    • Randomly shuffle all the words in the final message and display it.
  4. Ensure the program is interactive and user-friendly.


Bonus Challenges:

  1. Allow the user to save the generated messages to a text file.
  2. Implement a "retry" option to allow the user to create another message without restarting the program.

Example Output:

Choose a holiday template:
1. "Happy {holiday}, {name}! May your {wish} come true this season!"
2. "Wishing you a {adjective} {holiday}, {name}. Stay {wish} and joyful!"

Enter your choice: 1
Enter the holiday name: Christmas
Enter your name: Jessica
Enter an adjective: wonderful
Enter a wish: dreams

Generated Message:
"Happy Christmas, Jessica! May your dreams come true this season!"

Shuffled Words:
"come this May Jessica! true season dreams your Christmas, Happy"

Programming Languages:

You can complete this challenge in any programming language of your choice. Share your solution once you're done, and I can review it or suggest improvements!

CodeName: Jessica

💻 Linux Enthusiast | 🌍 Adventurer | 🦄 Unicorn 
🌐 My Site | 📢 Join the Forum

spacer.png

 

Posted

Here is an example of a solution in JavaScript:

 

const readline = require('readline');
const fs = require('fs');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

const templates = [
  "Happy {holiday}, {name}! May your {wish} come true this season!",
  "Wishing you a {adjective} {holiday}, {name}. Stay {wish} and joyful!"
];

function getUserInput(prompt) {
  return new Promise((resolve) => {
    rl.question(prompt, (answer) => {
      resolve(answer);
    });
  });
}

function generateMessage(template, holiday, name, adjective, wish) {
  return template
    .replace('{holiday}', holiday)
    .replace('{name}', name)
    .replace('{adjective}', adjective)
    .replace('{wish}', wish);
}

function shuffleMessage(message) {
  const words = message.split(' ');
  for (let i = words.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [words[i], words[j]] = [words[j], words[i]];
  }
  return words.join(' ');
}

async function main() {
  while (true) {
    console.log("\nChoose a holiday template:");
    templates.forEach((template, index) => {
      console.log(`${index + 1}. ${template}`);
    });

    const choice = parseInt(await getUserInput("\nEnter your choice: "), 10);
    if (isNaN(choice) || choice < 1 || choice > templates.length) {
      console.log("Invalid choice. Please choose a valid template number.");
      continue;
    }

    const holiday = await getUserInput("Enter the holiday name: ");
    const name = await getUserInput("Enter your name: ");
    const adjective = await getUserInput("Enter an adjective: ");
    const wish = await getUserInput("Enter a wish: ");

    const message = generateMessage(templates[choice - 1], holiday, name, adjective, wish);

    console.log("\nGenerated Message:");
    console.log(message);

    console.log("\nShuffled Words:");
    console.log(shuffleMessage(message));

    const saveOption = await getUserInput("\nWould you like to save the message to a text file? (yes/no): ");
    if (saveOption.toLowerCase() === 'yes') {
      fs.appendFileSync("holiday_message.txt", message + "\n");
      console.log("Message saved to holiday_message.txt.");
    }

    const retry = await getUserInput("\nWould you like to create another message? (yes/no): ");
    if (retry.toLowerCase() !== 'yes') {
      console.log("\nThank you for using the Holiday Message Generator! Happy Holidays!");
      break;
    }
  }
  rl.close();
}

main();

 

CodeName: Jessica

💻 Linux Enthusiast | 🌍 Adventurer | 🦄 Unicorn 
🌐 My Site | 📢 Join the Forum

spacer.png

 

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...

Important Information

Terms of Use Privacy Policy Guidelines We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.