0

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]);
        }
    }
});
1
  • 1
    Shuffle the array [0, ..., 9] and slice off the first four numbers. Commented Oct 30, 2013 at 1:34

3 Answers 3

2

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(''))
Sign up to request clarification or add additional context in comments.

1 Comment

+1 But you could get rid of the repetition if you use do-while instead of while.
1

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.

Comments

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]);
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.