0

I want to set a variable by running a function and retrieving a value.

This is my code (abbreviated if-checks):

var setToPage  = return loopHistory("external");

function loopHistory(scope) {
    for (var i = $.mobile.urlHistory.stack.length-2; i>=0; i--) {

        if (scope == "internal" ){
            if ( some[i] == thing ) || i == 0  ) {
                i == 0 ?  $temp =  "VALUE-ONE" : $temp =  "VALUE-TWO"
                return $temp;
                break;
                }
            } else if (scope == "external") {
                $temp = some[i];
                if ( thing == true )  {
                    return $temp;
                    break;
                    }
                }
        }
    }

This does not work.

Question:
How do I declare the value of setToPage by running the function above?

Thanks for help!

5
  • 1
    Hi Frequent, in the future, please tell us the error message. "It doesn't work" is not very informative. It would be like telling your mechanic that your car is broken without telling him/her what the symptoms are (doesn't start, makes wierd noise, check engine light on, etc) :) You're lucky Marc B has a sharp eye for detail ;) Commented May 26, 2012 at 20:33
  • 1
    @jmort253 he makes it more challenging (and fun) to omit those details though... Commented May 26, 2012 at 20:34
  • Remove the first return on the running of the function, and place a return statement outside the for loop, right now you're overwriting the $temp variable on each iteration, and it does'nt seem to be declared anywere, which would probably make it global aswell? Commented May 26, 2012 at 20:35
  • @adeneo: I declared $temp inside the function I'm trying to run this snippet in (should have included that I guess) Commented May 26, 2012 at 20:38
  • @ajax333221 - I can see that. Train hard :) Commented May 27, 2012 at 0:04

1 Answer 1

3

I don't know what the point of the return on your first line is... return is for inside a function, to return a value to whatever called the function.

var setToPage  = loopHistory("external");
Sign up to request clarification or add additional context in comments.

1 Comment

you have a point :-) (make that two). And I wrote elseif instead of else if. Let's see if that works.

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.