Created
June 16, 2016 14:14
-
-
Save daniellizik-sc/65a256431c166401b5c6c385dd09c3f2 to your computer and use it in GitHub Desktop.
unique promo code generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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