Skip to content

Instantly share code, notes, and snippets.

@djom202
Last active March 31, 2017 15:58
Show Gist options
  • Select an option

  • Save djom202/97abd6332d59269c26d4df4677aa7d44 to your computer and use it in GitHub Desktop.

Select an option

Save djom202/97abd6332d59269c26d4df4677aa7d44 to your computer and use it in GitHub Desktop.
Creating a distribution from an options number and an answers count to answer a Qrvey
function dist(num, answersCount){
var perc = Array.apply(null, { length: num }).map(function() {
return parseInt(answersCount / num);
}),
rest = answersCount - (parseInt(answersCount / num) * num);
perc[0] += rest; // for when is a only one result we always the first position.
return {
'perc': perc,
'answersCount': answersCount
};
};
var result = dist(7, 200);
console.log('last result', result);
// "last result" Object {
// answersCount: 200,
// perc: [32, 28, 28, 28, 28, 28, 28]
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment