Default | const determineWinner = function(match, tournament) {
let total = 0;
for (let i = 0; i < 3; i++) {
total += tournament.winPercentages[i];
if (Math.random() < total) {
return match[i];
}
}
}
determineWinner(["a", "b", "c"], {"winPercentages": [0.34, 0.33, 0.33]})
| ready |
Crazy person | const determineWinner = function(match, tournament) {
let total = 0;
total += tournament.winPercentages[0];
if (Math.random() < total) {
return match[0];
}
total += tournament.winPercentages[1];
if (Math.random() < total) {
return match[1];
}
total += tournament.winPercentages[2];
if (Math.random() < total) {
return match[2];
}
}
determineWinner(["a", "b", "c"], {"winPercentages": [0.34, 0.33, 0.33]})
| ready |
Crazy and caching random | const determineWinner = function(match, tournament) {
let total = 0;
const rand = Math.random()
total += tournament.winPercentages[0];
if (rand < total) {
return match[0];
}
total += tournament.winPercentages[1];
if (rand < total) {
return match[1];
}
total += tournament.winPercentages[2];
if (rand < total) {
return match[2];
}
}
determineWinner(["a", "b", "c"], {"winPercentages": [0.34, 0.33, 0.33]})
| ready |
Not crazy and caching random | const determineWinner = function(match, tournament) {
let total = 0;
const rand = Math.random();
for (let i in tournament.winPercentages) {
total += tournament.winPercentages[i];
if (rand < total) {
return match[i];
}
}
}
determineWinner(["a", "b", "c"], {"winPercentages": [0.34, 0.33, 0.33]})
| ready |