Web Analytics Made Easy -
StatCounter How to properly manipulate a funtions' arguments - CodingForum

Announcement

Collapse
No announcement yet.

How to properly manipulate a funtions' arguments

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to properly manipulate a funtions' arguments

    The Problem to be solved
    I want to call a function passing a set of parameters. Using the first parameter (argument[0] if you will) I check for a value-match; if there is a match I want to recursively call this function, dropping that first parameter while passing the rest. Continue recursive calls (dropping arguements[0] for the subsequent call) as long there are parameters and long as I'm matching that first parameter.

    What I think I know
    arguments is an object-type. It's not an Array, though it does have a length attribute. You cannot use Array methods directly on an arguments object.

    Why I'm scratching my head
    I'm not seeing a clean way to split the argument list. I don't seem to be doing it right.

    Or maybe the second, and subsequent, calls I'm not passing a comma delimited list in the call, rather a single parameter that is an Array.

    Or maybe I need to use the "callee" attribute somehow for recursive calls.

    I hesitate to show any code because it just seems like mush at this point.

    Maybe This is the way to do it?[list=1][*]Save each argument value in an Array[*]Build a string from the array, leaving out the first element of the array[*]The string will be a comma-delimited list of parameter values[*]But now I'm passing a string, not a list of values per se?? [*]Seems like a Catch-22![/list=1]

  • #2
    Maybe it is worth taking a step back and looking at THE PROBLEM you are trying to solve by the method you desribed...
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

    Comment


    • #3
      I hear what UR saying there V-guy...

      I have thought about it and my vision is a simple function call to find a node in a tree.

      For example I want to be able to say
      Code:
      bobsCarLot.getNode ('Chevy', 'Nova');
      You can imagine a tree structure where the root's (bobsCarLot in this case) children are car makes, "makes" children are car models, "models" children are whatever...

      You can see where a function such as this has utility for any tree-o-data where the logical structure (and values therein) are known.

      Further the getNode() method can be called for/from any given Node and the search begins there.

      Therefore I need to handle an indetermine number of parameters in the call. Additionally a recursive call seems to be the natrual way to handle traversing through an indeterminate number of tree branches.

      Comment


      • #4
        Re: How to properly manipulate a funtions' arguments

        Originally posted by RadarBob
        [*]The string will be a comma-delimited list of parameter values[*]But now I'm passing a string, not a list of values per se??
        Am I missing something?
        Why can't you take the comma delimitted string and turn it back into an array?

        eg:
        Code:
        <script>
           var myArr = ('a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z');
           var alpha = myArr.split(',');
           alert(alpha[3]);
        </script>
        .....Willy

        Comment


        • #5
          If all your arguments are strings why don't you combine them into one with some kind of separator:
          getNode('Chevy#Nova#1990')
          then split them into array which you can work with
          function getNode(argStr)
          { arguments = argStr.split('#');

          Use slice to remove members
          then bring them all into a string before making a recursive call...
          Vladdy | KL
          "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

          Comment


          • #6
            on example of recursive call until only 1 argument left weeding out the 1 argument each time. NOTE:this technique makes return unuseable

            Code:
            //Made by: Garadon :)(please delete this line)
            function wacky()
            {
             
             alert(arguments.length)
             if(arguments.length>1)
             {
               var str="";
               for(i=1;i<arguments.length;i++)
               {
                  str=str+arguments[i];
                  if(i!=arguments.length-1)
              	  {
                   str=str+',';
            	    }
                }
              	setTimeout('wacky('+str+');',0);
            	}
            }
            wacky(1,1,1,1,1,1);
            Last edited by Garadon; Feb 25, 2004, 01:34 PM.

            Comment


            • #7
              Willy, great minds think alike
              Note to self: learn to type faster.....
              Vladdy | KL
              "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

              Comment


              • #8
                This is a little O/T but I just learned something.
                I was not aware that you can pass an arguement to the split method.

                This alerts the first six characters:
                Code:
                <script>
                   var myArr =  ('a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z');
                   var alpha = myArr.split(',');
                   alert(myArr.split(',',6));
                </script>
                .....Willy

                Comment

                Working...
                X