代码:
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。
// 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];
}