Skip to content

Instantly share code, notes, and snippets.

@daniellizik-sc
Created June 16, 2016 14:14
Show Gist options
  • Save daniellizik-sc/65a256431c166401b5c6c385dd09c3f2 to your computer and use it in GitHub Desktop.
Save daniellizik-sc/65a256431c166401b5c6c385dd09c3f2 to your computer and use it in GitHub Desktop.
unique promo code generation
/**
* takes email and returns unique id, may need .reduce polyfill for < ie8
*
* @param {string} str - an email address (or any string) to convert to unqiue id
* @returns {string} unique identifier
*/
function promoGen(str) {
var data = str.split('').reduce(function (acc, s, i) {
var code = s.codePointAt();
acc.sum += code;
acc.slope += code * i;
return acc;
}, { sum: 0, slope: 0 });
return data.sum + '-' + data.slope + '-' + str.length;
}
const promoGen = (str) => {
const data = str.split('').reduce((acc, s, i) => {
const code = s.codePointAt();
acc.sum += code;
acc.slope += code * i;
return acc;
}, {sum: 0, slope: 0});
return `${data.sum}-${data.slope}-${str.length}`;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment