Skip to content

Instantly share code, notes, and snippets.

@bustapaladin
Forked from ruzli/median_logging.js
Created May 26, 2019 13:09
Show Gist options
  • Save bustapaladin/35053cc90ab050d4f98575f9793e9792 to your computer and use it in GitHub Desktop.
Save bustapaladin/35053cc90ab050d4f98575f9793e9792 to your computer and use it in GitHub Desktop.
Median logging in console and log output
Array.prototype.median = function (arr) {
arr.sort((a, b) => a - b);
var pivot = Math.floor(arr.length / 2);
return arr.length % 2 ? arr[ pivot ] : (arr[ pivot - 1 ] + arr[ pivot ]) / 2;
}
class Median{
constructor(context) {
this.context = context
this.history = []
}
show(multiplier){
this.history.push(multiplier)
let games_amount = this.history.length
this.context.log(`Rolls: ${games_amount} | Median: ${this.history.median(this.history).toFixed(2)}`)
console.log(`Rolls: ${games_amount} | Median: ${this.history.median(this.history).toFixed(2)}`)
}
}
var median = new Median(this)
while(true){
const {multiplier} = await this.bet(100, 1.01)
median.show(multiplier)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment