Git Product home page Git Product logo

secret-santa's Introduction

Secret Santa Game

This program assigns secret children to each employee for a Secret Santa game. It takes a list of employees and their email IDs, along with the previous year's assignments, and generates new assignments for the current year.

Solution Overview

  1. The solution uses the fs module and csv-parser library to read and parse CSV files.
  2. The readCsv(fileName) function reads a CSV file and returns a promise that resolves to an array of objects, where each object represents a row in the CSV file.
  3. The assignSecretChildren(employees, previousAssignments) function takes an array of employee objects and an array of previous assignment objects as input. It generates new secret Santa assignments for each employee, avoiding any conflicts with previous assignments. It returns an array of new assignment objects.
  4. The getRandomEmployee() function is a helper function used by assignSecretChildren() to randomly select an employee for assignment, ensuring that the selected employee meets all the assignment criteria.
  5. The isAssignedPreviously() and isAssignedInCurrentYear() functions are helper functions used to check if an employee has been assigned to the same employee previously or to another employee in the current year, respectively.
  6. The writeSecretSantaAssignments(assignments, fileName) function takes an array of assignment objects and a file name as input. It generates a new CSV file with the assignments.
  7. The validateCsvData(data, expectedFields) function is a helper function used to validate the CSV data and ensure that it has the expected format and fields.
  8. The runSecretSantaGame(employeeListFile, previousAssignmentsFile, resultFile) function is the main function that orchestrates the Secret Santa game. It reads the employee list and previous assignment files, validates the data, generates new assignments, and writes the result to a new CSV file.

Installation

To use this program, follow these steps:

Clone the repository or download the code files.

Install the required dependencies by running the following command in your terminal:

npm install

Usage

To run the Secret Santa game, you need to provide two input files: Employee-List.csv and Secret-Santa-Game-Result-2023.csv. These files should be in CSV format and have specific columns.

  1. Employee-List.csv - This file contains the list of employees participating in the game. It should have the following columns:
  • Employee_Name - The name of the employee
  • Employee_EmailID - The email ID of the employee

Example format:

Employee_Name,Employee_EmailID
Alice,[email protected]
Bob,[email protected]
Charlie,[email protected]
  1. Secret-Santa-Game-Result-2023.csv - This file contains the previous year's assignments. It should have the following columns:
  • Employee_Name - The name of the employee
  • Employee_EmailID - The email ID of the employee
  • Secret_Child_Name - The name of the assigned secret child for the employee
  • Secret_Child_EmailID - The email ID of the assigned secret child

Example format:

Employee_Name,Employee_EmailID,Secret_Child_Name,Secret_Child_EmailID
Alice,[email protected],Bob,[email protected]
Bob,[email protected],Charlie,[email protected]
Charlie,[email protected],Alice,[email protected]
  1. Secret-Santa-Game-Result-2024.csv - This file will contain the new assignments for the current year. It will be generated by the program.

After preparing the input files, open the index.js file and modify the following line to provide the correct filenames:

runSecretSantaGame(
  "Employee-List.csv",
  "Secret-Santa-Game-Result-2023.csv",
  "Secret-Santa-Game-Result-2024.csv"
);

Save the file, and then execute the program by running the following command:

node index.js

The program will read the input files, generate new assignments, and save them to the Secret-Santa-Game-Result-2024.csv file.

Testing

The program includes a test file named secretSanta.test.js to ensure the correctness of the assignSecretChildren function. To run the tests, execute the following command:

npm test

The tests will be executed, and the results will be displayed in the console.

Additional Information

The assignSecretChildren function is responsible for assigning secret children to each employee based on certain rules.

  • An employee cannot choose themselves as their secret child.
  • An employee cannot be assigned to the same secret child as in the previous year's Secret Santa event, if applicable.
  • Each employee must have exactly one secret child.
  • Each secret child should be assigned to only one employee. It takes the list of employees and previous assignments as input and returns the new assignments.

The program uses the csv-parser and fs modules to read and write CSV files, respectively. These dependencies are already installed when you run npm install.

The validateCsvData function is used to validate the CSV data and ensure it meets the expected format.

The program handles errors during file reading, writing, and data validation. If any errors occur, they will be logged to the console.

For more details, please refer to the code files and comments provided.

secret-santa's People

Contributors

kirtanmodi avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.