How to generate referral codes in javascript


How to generate referral codes in javascript

You have to generate referral codes and for sure you want a quick answer, right?

Here’s my code which generate referral codes and save them in a txt file.

const fs = require('fs');

const CODE_LENGTH = 6;
const TOTAL_CODES_GENERATED = 100;

function generateCode(length) {
  const charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  let retVal = '';
  for (let i = 0, n = charset.length; i < length; ++i) {
    retVal += charset.charAt(Math.floor(Math.random() * n)).toUpperCase();
  }
  return retVal;
}

for (let index = 0; index < TOTAL_CODES_GENERATED; index++) {
  const code = `${generateCode(CODE_LENGTH)}\n`;
  fs.appendFile('codes.txt', code, (err) => {
    if (err) throw err;
  });
}

The length of the code can be changed using CODE_LENGTH, and the numbers of code can be changed by TOTAL_CODES_GENERATED.

Save this in a js file, for example generate-referral-codes.js, and then execute the file

node generate-referral-codes.js

In the same directory you’ll have now a generated codes.txt file with all the codes.

That’s it!

Image by Tim from Pixabay

Newsletter


Related Posts

Tools for Analyzing Your Competition

Before knowing where your business stands, you should look at the market and your competition. You need to know what marketing tactics and strategies are working for your audience, what traffic the competition has, and how they position.

Free Online Photo Editor with Cool Effects | Edit Images Easily

Transform your images with our free online photo editor with cool effects. Add filters, adjust colors, and apply creative enhancements. Edit your photos easily and make them stand out.

How to find the size of a table in SQL?

Learn how to determine the size of a SQL table with this informative guide. Discover efficient methods for measuring table size.

MongoDB Query date by _id field

Query date by _id in mongodb. Straighforward guide to solve your issue right away.

Unleashing the Power of Free API for Scrape Reverse Geocoding

Learn how to harness the potential of reverse geocoding with our beginner's guide to scraping APIs for free. Unlock the power today.

Discover the Power of Whatsapp-web.js for Safe and Easy Automation

Automate WhatsApp with ease using Whatsapp-web.js, a NodeJS client library that connects through the official WhatsApp Web app, reducing ban risks. Perfect for user or business accounts.

Website tours js tools

Tools to create live tours for your web project using js libraries.

Unlocking SaaS Marketing Success: Essential Resources to Learn and Grow

Discover a comprehensive collection of valuable resources to master the art of SaaS marketing.

Where to launch your startup

Do you wanna launch grandiosely your startup? Get a list of platforms where people can discover your project and can push your project directly to the sky.

Email campaign improving my SaaS convertion - Manager Flota

Users subscribe to your SaaS, but don't let the money on your table. Here's how I do it!