0

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);
}
5
  • 4
    should the alert be inside the function? Commented Sep 18, 2013 at 15:26
  • possible duplicate of JavaScript equivalent of PHP's in_array() Commented Sep 18, 2013 at 15:27
  • Shouldn’t the returned take care of that ? Anyways I tried that, but still no luck :( Commented Sep 18, 2013 at 15:29
  • @AlejandroReinel: The return returns the value. It doesn't let you access private function variables. You want alert(test()). Commented Sep 18, 2013 at 15:35
  • @Martijn not a duplicate, because I need a number that is not included in my array, not only to check if its there Commented Sep 18, 2013 at 15:48

6 Answers 6

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

5 Comments

The return in the if will bail out of this function before alerting.
I see twice 'var n' ;)
but i am still getting number that are included in my array in the alert
@AlejandroReinel can u explain what you need or make a new question.
@TusharGupta 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!!
1

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.

2 Comments

okay, i fixed the variable problem, but i thougt the if/else statments where creating a loop
That's not how if/else works. If you want a loop, you need an actual loop, using 'for' or 'do/while' for example.
0

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

2 Comments

Dosent var make it global?
No, var does not make a global, it simply declares a variable
0

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

Comments

0

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
    }

Comments

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 {
        test();
    }
}

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.