简体   繁体   中英

$.inArray() jQuery function

I can not figure out why this is not working, should be returning an array with four distinct values, but it doesn't

$(document).ready(function (e) {
    var randomNumbers = new Array();
    for (var i = 0; i < 4; i++) {
        randomNumbers[i] = Math.floor((Math.random() * 9) + 1);
        while ($.inArray(randomNumbers[i], randomNumbers) !== -1) {
            randomNumbers[i] = Math.floor((Math.random() * 9) + 1);
        }
    }
    for (var i = 0; i < randomNumbers.length; i++) {
        if ($('#output').html() !== '') {
            var existingOutput = $('#output').html();
            $('#output').html(existingOutput + randomNumbers[i]);
        } else {
            $('#output').html(randomNumbers[i]);
        }
    }
});

Can cut out the if and the second loop by appending the joined array

$(document).ready(function (e) {
    var randomNumbers = new Array();
    for (var i = 0; i < 4; i++) {
       var ran =newNum();
       /* unique check*/
       while ( $.inArray( ran, randomNumbers) >-1){
          ran=newNum();
       }
        randomNumbers.push(ran)
    }
   $('#output').append( randomNumbers.join(''))

});

function newNum(){
  return Math.floor((Math.random() * 9) + 1);
}

Alternate solution using a shuffle method ( found in this post ):

var a=[1,2,3,4,5,6,7,8,9];
function Shuffle(o) {
    for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
    return o;
};

 $('#output').append( Shuffle(a).splice(0,4).join(''))

If you generate a number and put it in the array , don't you think that $.inArray() will tell you so?

Your while loop is guaranteed to hang. A member of the array ( randomNumbers[i] ) is always, of course, going to be in the array. In fact $.inArray() when called to see if randomNumbers[i] is in the array will return i (if it's nowhere else, which in this case it can't be). Your loop won't get past the first number, so it'll just be 0 .

I don't understand the point of your while loop. inArray only returns -1 if the value isn't found, which it will always be found, so you're just creating an infinite loop for yourself that will keep resetting the random number generated.

If you're just trying to add four random numbers to a div , this worked for me:

$(document).ready(function (e) {
    var randomNumbers = new Array();
    for (var i = 0; i < 4; i++) {
        randomNumbers[i] = Math.floor((Math.random() * 9) + 1);
    }
    for (var i = 0; i < randomNumbers.length; i++) {
        if ($('#output').html() !== '') {
            var existingOutput = $('#output').html();
            $('#output').html(existingOutput + randomNumbers[i]);
        } else {
            $('#output').html(randomNumbers[i]);
        }
    }
});

Further refactored:

$(document).ready(function (e) {
    var randomNumbers = new Array();
    for (var i = 0; i < 4; i++) {
        randomNumbers[i] = Math.floor((Math.random() * 9) + 1);
    }

    for (var i = 0; i < randomNumbers.length; i++) {    
        $('#output').append(randomNumbers[i]);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM