Alejandro Reinel
Alejandro Reinel

Reputation: 57

Return value if it does not exist in array

I need this code to return a different value (from 0 to 14) if its not defined in my array. I dont understan why it tells me that "n" is not defined. please help

EDIT ***

I fixed the n value, but I’m still getting numbers that are included in my array !!

EDIT 2 *******

I need my code to return a numerical value (at random) that is not included in my array. This is a simplified version of what I’m doing, the full version has non sequential values and a lot of numbers!!

function test () {
   var arr= [1,2,3,4,5,6,7,8,9];
   var n = Math.floor((Math.random() * 15));
   var tex = $.inArray( n, arr );
   if (tex == -1) {
       return n;
   }
   else {
       var n = Math.floor((Math.random() * 15));
   }
  alert (n);
}

Upvotes: 0

Views: 2035

Answers (6)

Akash Sarawagi
Akash Sarawagi

Reputation: 207

function test() {
    var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    var n = Math.floor((Math.random() * 15));
    var tex = $.inArray(n, arr);
    if (tex == -1) {
        return n;
    } else {
        test();
    }
}

Upvotes: 0

function test () {
   var arr= [1,2,3,4,5,6,7,8,9];
   var n = Math.floor((Math.random() * 15));
   var tex = $.inArray( n, arr );
   if (tex == -1) {
       return n;
   }
   else {
       n = Math.floor((Math.random() * 15)); //no need of declaring n again
   }
   alert (n);//alert should be here inside the function 
}

n is defined inside the function test

so var n scope is only inside the function i.e n is not accessible outside the function test

in your code alert (n); will only work if the function fails if condition.As if conditions becomes true it will returns n control will be out of the function i.e no further execution of the code inside the function.

Read --> What is the scope of variables in JavaScript?

Updated after OP's comment

DEMO

function test() {
    var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    var n = Math.floor((Math.random() * 15));
    var tex;
    while ((tex = $.inArray(n, arr)) != -1) {
        n = Math.floor((Math.random() * 15));
    }
    return n;
}
alert(test());

Upvotes: 4

Sean Burton
Sean Burton

Reputation: 961

Aside from the fact that the variable goes out of scope when the function ends, as everyone else has mentioned, your function does not do what you say it is supposed to do. If the first number it generates is in the array it only makes one more attempt at generating a random number, then uses this number regardless of whether it is in the array or not. You need some sort of loop to keep attempting new random numbers until it finds one that is definitely not in the array.

Upvotes: 1

neoeahit
neoeahit

Reputation: 1799

Firstly you mentioned that you want to return a value: why are you making it global?

Try this:

 function test () {
       var arr= [1,2,3,4,5,6,7,8,9];
       n = Math.floor((Math.random() * 15));
       var tex = $.inArray( n, arr );
       if (tex == -1) {
           return n;
       }
       else {
          n = Math.floor((Math.random() * 15));
       }
return n
    }

Upvotes: 0

lukas.pukenis
lukas.pukenis

Reputation: 13597

Your n variable is defined inside a function scope thus is not accesible from the global scope.

To return values from function use this syntax:

return n;

so then you can do something like this in global scope:

alert(function(args));

Upvotes: 0

tymeJV
tymeJV

Reputation: 104775

n is local to the function it is defined in, define n outside your function to use it outside the function.

Upvotes: 0

Related Questions