Source: pages-sections/SignUp-Sections/generateCode.js

/**
 * The `generateCode` function generates a random code of a specified length.
 * @param length - The length of the code to be generated.
 * @returns The function returns a randomly generated code of the specified length.
 * The code consists of alphanumeric characters (uppercase and lowercase letters, and numbers).
 * The code is generated by randomly selecting characters from the alphanumeric character set.
 * The function iterates through the character set to create the code of the specified length.
 * The generated code is then returned as the output of the function.
 *
 */

export function generateCode(length) {
  let result = "";
  const characters =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  const charactersLength = characters.length;
  let counter = 0;
  while (counter < length) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength));
    counter += 1;
  }
  return result;
}