コード:
function maxStyle() {
var one = parseInt($("#num-one").val()),
two = parseInt($("#num-two").val()),
three = parseInt($("#num-three").val()),
four = parseInt($("#num-three").val()),
five = parseInt($("#num-three").val());
// ..etc.
var arr = [one, two, three, four, five /*, etc. */],
tempArr = [];
arr.sort(function(a, b) {
return (b - a)
}); // now array is sorted with highest element on top
arr[0] = arr[0] + 1; // increment the highest element by 1, per question
var a = 0, b;
for (b = 0; b < arr.length; b++ ) {
if(arr[a] == arr[b]) {
tempArr.push(arr[b]);
a++;
// not incrementing outside the loop,
// since we don't want to compare other duplicates,
// but only the largest which is the first element
}
}
// We now have an array of multiple items equal to the max
最大値に等しい変数が複数ある場合
は、変数の 1 つをランダムに選択し、それに +1 します。
// Continue Code..
// Generate a random between 1 and no. of items
randomItem = Math.floor(Math.random() * arr.length) + 1;
tempArr[randomItem] = tempArr[randomItem] + 1;
return arr[0] > tempArr[randomItem] ? arr[0] : tempArr[randomItem];
}