1

I am generating a list of random numbers. Each random number is added to an array, but I want to check that the same number isnt entered twice. I am having a lot of trouble trying to get the if statement to work with this and am not sure what I have done wrong.

I have created:

//INITIALISE VARS, ARRAYS
var uniquearr = [];
i = 0;

while (i < 30){

    var min = 0;
    var max = 29;
    var random = Math.floor(Math.random() * (max - min + 1)) + min;

    //SEARCH UNIQUE ARRAY FOR EXISTING
    if (jQuery.inArray(random, uniquearr) > -1){

        //ADD NUMBER TO UNIQUE ARRAY
        uniquearr.push(random);

        //*DO SOMETHING*

    } //END IF

    i++;

} //END WHILE

But the if statement never triggers. Can anyone point me in the right direction?

3
  • 1
    Maybe you should check if (jQuery.inArray(random, uniquearr) < 0) { } Commented Jul 8, 2013 at 6:43
  • he should also use correct code indenting, which should have made the error on the END IF line and END WHILE lines obvious. Commented Jul 8, 2013 at 6:46
  • 1
    Your condition is reversed, it will only add random if it already exists in the array. Commented Jul 8, 2013 at 6:47

2 Answers 2

3

You need to test whether the random number does not exist in the array; only then should it be added to the array.

Also another problem with the logic was, you were not adding the 30 unique numbers always as the i variable was incremented outside the if condition. Here you do not have to use a different loop variable since you can check whether the destination array is of desired size

//INITIALISE VARS, ARRAYS
var uniquearr = [], min = 0, max = 29;

//SEARCH UNIQUE ARRAY FOR EXISTING
while (uniquearr.length < 30){
    var random = Math.floor(Math.random() * (max - min + 1)) + min;
    if (jQuery.inArray(random, uniquearr) == -1){
        uniquearr.push(random);
    }//END IF

}//END WHILE

console.log('uniquearr', uniquearr)
Sign up to request clarification or add additional context in comments.

Comments

2

That's because your if statement always is false, your array is empty and as result $.inArray always returns -1, you should check whether the returned value is -1 or not.

while (uniquearr.length < 30) { // uniquearr.length !== 30
    var min = 0,
        max = 29,
        random = Math.floor(Math.random() * (max - min + 1)) + min;
    //SEARCH UNIQUE ARRAY FOR EXISTENCE
    if (jQuery.inArray(random, uniquearr) === -1) {
        //ADD NUMBER TO UNIQUE ARRAY
        uniquearr.push(random);
    } 
} 

http://jsfiddle.net/zzL7v/

2 Comments

best code answer, but now please actually tell the OP what he did wrong, too.
if you check array length remove i++;, and also remove ; at the end of the while and if blocks...

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.