JQuery Max Number +随机化

时间:2012-12-31 18:50:41

标签: javascript jquery

function maxStyle() {
    var one = parseInt($("#num-one"));
    var two = parseInt($("#num-two"));
    var three = parseInt($("#num-three"));
}
  1. 找到最大数字和+1变量。
  2. 如果有多个变量等于max,则随机选择一个变量,然后选择+1。
  3. 我知道我可以使用Math.max() - 但这只返回一个数字,那么如何获取变量呢?

    对于提前的无趣感到抱歉,我只用了4天的JS / JQ。

5 个答案:

答案 0 :(得分:1)

您可以执行此操作:http://jsfiddle.net/zZpVq/1/

var max = -Infinity; // variable to store max value;

var maxEle = $(".num").each(function() {
    if (+this.value > max) max = +this.value;  // find the max value
}).filter(function() {
    return this.value == max;  // filter elements to ones that share max value
});

if (maxEle.length > 1)   // get a random element with the max value
    maxEle = maxEle.eq(Math.floor(Math.random() * maxEle.length));

maxEle.css("border-color", "red");​ // do something with that element.

修改 - 将初始max更改为-Infinity并强制使用一元加号进行this.value的数字转换,这样也可以使用负值。

答案 1 :(得分:1)

代码:

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
  

如果有多个变量等于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];
}

答案 2 :(得分:0)

我相信这就是你要做的......

function maxStyle() {
  var one = parseInt($("#num-one").val());
  var two = parseInt($("#num-two").val());
  var three = parseInt($("#num-three").val());
  var maxed = Math.max(one, Math.max(two, three)) + 1; // Max of all three plus 1
}

变量maxed将具有三个输入中最大值的值...假定它们是输入。

我不确定随机化的位置(来自你的问题标题)。

答案 3 :(得分:0)

第一期

parseInt($("#num-one")); 

你是如何从jQuery对象中获取数字的?

您应该使用.val()或.text()

parseInt($("#num-one").val(),10); 

使用parseInt时,请使用基数。

现在针对其他问题,您需要使用一些if语句。

答案 4 :(得分:0)

我只需设置一个包含所有ID的数组,并将它们与存储的最大值进行比较。

var idArr = ['#num-one', '#num-two', '#num-three'];

function maxNum(idArr) {
  var max = 0;
  $.each( idArr, function( key, value ) {
    var curr = parseInt($(value).val(),10);
    if (curr > max) max = curr;
  });
  return max+1;
}

alert('Max :' + maxNum(idArr));
相关问题