A Pen by Vinayak Humberi on CodePen.
Created
August 14, 2019 05:53
-
-
Save vinayakhumberi/aa52ff751dff31724c107a6c5f3100f4 to your computer and use it in GitHub Desktop.
Traffic distribution logic
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
<div> | |
<pre id="result"></pre> | |
</div> |
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
const bckts = { "A": 6, "B": 3, "C": "1"}; | |
const Sorter = function (buckets, bucketNames) { | |
this._resBktArr = buckets; | |
this._bktArr = this._resBktArr.slice(); | |
this._resBktName = bucketNames; | |
this._bktName = this._resBktName.slice(); | |
this._cntr = -1; | |
this.tellMeWhereIBelong = function () { | |
this._cntr += 1; | |
const sandbox = this._cntr % 10; | |
const index = sandbox % this._bktArr.length; | |
const rtrnEle = this._bktName[index]; | |
if (index > -1) { | |
this._bktArr[index] = this._bktArr[index] - 1; | |
} | |
if (this._bktArr[index] === 0) { | |
const t = this._bktArr.splice(index, 1); | |
this._bktName.splice(index, 1); | |
if (this._bktArr.length === 0) { | |
this._bktArr = this._resBktArr.slice(); | |
this._bktName = this._resBktName.slice(); | |
} | |
} | |
return rtrnEle; | |
}; | |
}; | |
const node = new Sorter(Object.values(bckts), Object.keys(bckts)); | |
let requests = 0; | |
const interval = setInterval(() => { | |
requests++; | |
document.getElementById("result").append(node.tellMeWhereIBelong()+ ' ') | |
if(requests % 10 === 0) { | |
document.getElementById("result").append('\n') | |
} | |
if (requests === 120) { | |
clearInterval(interval); | |
} | |
}, 100); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment