Simple question about functions/strings

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phub11
    New Member
    • Feb 2008
    • 127

    Simple question about functions/strings

    This seems a really simple question to answer, but being fairly new to JS, I have no idea how to do it:

    I'd like to insert a string which contains JS commands set by one function into another function. Below is sample code which hopefully explains what I want to do:

    Code:
    func1 () {
    ---blah---
    if (t == 1) {
    test = "
    if (j1=="right" && i1<24) {
    i1 = parseInt(i1)+1;
    } else {
    i1 = parseInt(j1); }
    ";
    ---blah---
    OR SET test USING OTHER CONDITIONS
    ---blah---
    }
    
    funcA() {
    ---blah---
    INSERT test HERE
    ---blah---
    }
    Probably alarmingly simple, but can't find any example code.

    Thanks
  • iam_clint
    Recognized Expert Top Contributor
    • Jul 2006
    • 1207

    #2
    well you cannot make one function out of another however this may not be what your asking.. to set a global variable you put var test; outside of the functions like so..

    [CODE=javascript]
    <script>
    var test;
    function a() {
    test="blah!";
    }

    function b() {
    alert(test);
    }
    </script>
    [/CODE]

    We can help get you a solution to fix your problem if you care to explain what you are really trying to accomplish.

    Comment

    • rnd me
      Recognized Expert Contributor
      • Jun 2007
      • 427

      #3
      higher-order functions are indeed possible in javascript, as are dynamic functions:

      Code:
      var funcA= new Function(test);
      alert(funcA.toString());

      Comment

      • phub11
        New Member
        • Feb 2008
        • 127

        #4
        Thanks for the replies.

        Maybe I'm going about this wrong. Let me try to explain what I want to do:

        I have a function within which a number of "constants" are set depending on which value a pull down is set to. To avoid setting about 10 "constants" (based on the pull down selected), I thought I could drop in code like a string.

        So....

        If the drop down (t) is set to 1, include the following code at a specific point in funcA (a different function):

        [CODE]
        if (j1=="right" && i1<24) {
        i1 = parseInt(i1)+1;
        } else if (j1=="down" && i1<19) {
        i1 = parseInt(i1)+6;
        } else {
        i1 = parseInt(j1); }
        [CODE]

        If the drop down (t) is set to 2, include the following code at the same specific point in funcA:

        Code:
        if (j1=="right" && i1<48) {
        i1 = parseInt(i)+1;
        } else if (j1=="down" && i1<43) {
        i1 = parseInt(i1)+6;
        } else {
        i1 = parseInt(j1); }
        There are actually a lot more "else if" statements, so it would get quite difficult to set "24/48", "19/43", etc, as variables --- I think!

        Thanks!

        Comment

        • phub11
          New Member
          • Feb 2008
          • 127

          #5
          Thinking about it, those number are all related, so I guess I could pass a variable and use operators to figure them all out.

          However, for future reference though, could someone please tell me if the above question is possible.

          Comment

          • iam_clint
            Recognized Expert Top Contributor
            • Jul 2006
            • 1207

            #6
            Originally posted by phub11
            Thanks for the replies.

            Maybe I'm going about this wrong. Let me try to explain what I want to do:

            I have a function within which a number of "constants" are set depending on which value a pull down is set to. To avoid setting about 10 "constants" (based on the pull down selected), I thought I could drop in code like a string.

            So....

            If the drop down (t) is set to 1, include the following code at a specific point in funcA (a different function):

            Code:
            if (j1=="right" && i1<24) {
            i1 = parseInt(i1)+1;
            } else if (j1=="down" && i1<19) {
            i1 = parseInt(i1)+6;
            } else {
            i1 = parseInt(j1); }
            If the drop down (t) is set to 2, include the following code at the same specific point in funcA:

            Code:
            if (j1=="right" && i1<48) {
            i1 = parseInt(i)+1;
            } else if (j1=="down" && i1<43) {
            i1 = parseInt(i1)+6;
            } else {
            i1 = parseInt(j1); }
            There are actually a lot more "else if" statements, so it would get quite difficult to set "24/48", "19/43", etc, as variables --- I think!

            Thanks!

            You could always put your numbers into an array and then loop through the array with a for loop. Example
            [CODE=javascript]
            <script>
            var n1 = [48,43,22,12,23, 13];
            var i1 = 48;
            var j1 = "down";
            for (i=0; i<n1.length; i++)
            {
            if (j1=="down" && i1==n1[i])
            {
            alert(n1[i]);
            //do stuff
            }
            }
            </script>
            [/CODE]

            Comment

            • rnd me
              Recognized Expert Contributor
              • Jun 2007
              • 427

              #7
              why don't you simply make a couple extra functions with the code needed for each drop down position. in your function, you can then call that wrapper function in the middle of another function. this forces the script to evaluate the code later, after the surrounding variables have been set.

              example
              Code:
              function ver1(a){ return a + 10 }
              function ver2(a){ return a + 50 }
              
              function testMe(arg){
                var a = parseInt(arg);
                if(a>10) return ver1(a)
                if(a<10) return ver2(a)
              }

              Comment

              Working...