kulmapaikka-ircbot/plugins/getDice.js
2015-09-01 22:44:55 +03:00

36 lines
782 B
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

module.exports = function(config) {
var getDiceString = function(numThrows, sides) {
var total = 0;
var totalThrows = [];
numThrows = Number(numThrows);
sides = Number(sides);
if(numThrows === 0 || sides === 0) {
return 'Tyhjää ei voi heittää';
}
if(numThrows > 100) {
return 'Ei jaksa...';
}
for(var a = 0; a < numThrows; a++) {
var t = Math.floor(Math.random()*sides)+1;
totalThrows.push(t);
total += t;
}
var resultString = '';
totalThrows.forEach(function(value) {
resultString += value + ', ';
});
resultString = resultString.substring(0, resultString.length - 2);
resultString += ' (yhteensä: ' + total + '. '+ (total/numThrows).toFixed(2) + ' per heitto)';
return resultString;
};
return getDiceString;
};