Created
November 3, 2019 05:11
-
-
Save segdeha/a45ef8d6d7f094f4038a2318a9d60051 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Calculate a weighted estimate for the interval until the next purchase | |
* Current purchase a tiny bit less weight than all previous purchases | |
* @param {Number} lastEstimate The last stored purchase interval estimate | |
* @param {Number} latestInterval The interval between the most recent and previous purchases | |
* @param {Number} numberOfPurchases Total number of purchases for the item | |
*/ | |
const calculateEstimate = (lastEstimate, latestInterval, numberOfPurchases) => { | |
if (isNaN(lastEstimate)) { | |
lastEstimate = 14; | |
} | |
// FIXME algorithm doesn't work when there's only 1 purchase in the database | |
let previousFactor = lastEstimate * numberOfPurchases; | |
let latestFactor = latestInterval * (numberOfPurchases - 1); | |
let totalDivisor = numberOfPurchases * 2 - 1; | |
return (previousFactor + latestFactor) / totalDivisor; | |
}; | |
export default calculateEstimate; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment