Better (?) weight matching when desired weight is unavailable
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
/* | |
* See www-style | |
* @param req: Desired weight. A multiple of 100 in [100-900] | |
* @param available: An array of multiples of 100 in [100-900] | |
* @return The weight picked or -1 if falling back is better | |
* Call like matchWeight(400, [100, 600, 900]) | |
*/ | |
function matchWeight(req, available) { | |
if (available.indexOf(req) > -1) { | |
return req; | |
} | |
for (o = -100; Math.abs(o) <= 300; o = -o - (o < 0? 0 : 100)) { | |
if (available.indexOf(req + o) > -1) { | |
return req + o; | |
} | |
} | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment