returning a list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff Thies

    returning a list

    I'd like to return two values from a function. I'm doing this:

    function getTwo(){
    // some code
    return new Array(value_a,v alue_b);
    }

    var returnArray=get Two(some_var);

    var value_a=returnA rray[0];
    var value_b=returnA rray[1];

    Is there a cleaner way of doing that? I'm used to perl list assignments.

    Cheers,
    Jeff


  • Dr John Stockton

    #2
    Re: returning a list

    JRS: In article <0sjEb.3675$wL6 .1982@newsread1 .news.atl.earth link.net>,
    seen in news:comp.lang. javascript, Jeff Thies <nospam@nospam. net> posted
    at Thu, 18 Dec 2003 15:10:52 :-[color=blue]
    > I'd like to return two values from a function. I'm doing this:
    >
    >function getTwo(){
    >// some code
    >return new Array(value_a,v alue_b);
    >}
    >
    >var returnArray=get Two(some_var);
    >
    >var value_a=returnA rray[0];
    >var value_b=returnA rray[1];
    >
    > Is there a cleaner way of doing that? I'm used to perl list assignments.[/color]

    Yes. You could have written return [value_a, value_b] } .

    However, if the result is not naturally an array, consider

    ... return { A:value_a, B:value_b } }

    then with (getTwo(some_va r)) { var value_a = A, value_b = B }

    or Ob = getTwo(some_var )
    and then work with Ob.A & Ob.B .

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
    <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
    <URL:http://www.merlyn.demo n.co.uk/js-index.htm> Jsc maths, dates, sources.
    <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/Jsc/&c, FAQ topics, links.

    Comment

    • Jeff Thies

      #3
      Re: returning a list

      > However, if the result is not naturally an array, consider[color=blue]
      >
      > ... return { A:value_a, B:value_b } }
      >
      > or Ob = getTwo(some_var )
      > and then work with Ob.A & Ob.B .[/color]

      That's really nice! I'm calling a function that calculates many different
      properties. Now I can retrieve any of these that are in the hash. Perfect
      for astronomical calculations.

      BTW. Wanted to thank you for the javascript time/date FAQ. Used it a bunch
      yesterday!

      Cheers,
      Jeff
      [color=blue]
      > --
      > © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE[/color]
      4 ©[color=blue]
      > <URL:http://jibbering.com/faq/> Jim Ley's FAQ for[/color]
      news:comp.lang. javascript[color=blue]
      > <URL:http://www.merlyn.demo n.co.uk/js-index.htm> Jsc maths, dates,[/color]
      sources.[color=blue]
      > <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/Jsc/&c, FAQ topics,[/color]
      links.


      Comment

      • Grant Wagner

        #4
        Re: returning a list

        Jeff Thies wrote:
        [color=blue][color=green]
        > > However, if the result is not naturally an array, consider
        > >
        > > ... return { A:value_a, B:value_b } }
        > >
        > > or Ob = getTwo(some_var )
        > > and then work with Ob.A & Ob.B .[/color]
        >
        > That's really nice! I'm calling a function that calculates many different
        > properties. Now I can retrieve any of these that are in the hash. Perfect
        > for astronomical calculations.
        >
        > BTW. Wanted to thank you for the javascript time/date FAQ. Used it a bunch
        > yesterday!
        >
        > Cheers,
        > Jeff[/color]

        If you know what you want to return, such as the description of a star, I would
        strongly urge you to create an object Star, then return an instance of that
        object. I don't know the properties of a star, but the code would look something
        like:

        function Star(distance, brightness, temperature) {
        this.distance = distance;
        this.brightness = brightness;
        this.temperatur e = temperature;

        this.setDistanc e = function(d) {
        if (d > 1000000000) {
        return -1;
        } else {
        this.distance = d;
        return this.distance;
        }
        }
        this.getDistanc e = function() {
        return this.distance + 'au';
        }
        this.getBrightn ess = function() {
        return this.brightness ;
        }
        this.getTempera ture = function() {
        return this.temperatur e;
        }
        }

        Then in your function that returns information about a star, it could do
        something like:

        return new Star(1, 2, 3);

        And the code that uses the returned value would be:

        var someStar = someFunctionTha tReturnsAStarOb ject();
        document.write( 'The star is ' + someStar.getDis tance() + ' away');
        if (someStar.setDi stance(10000000 0000) == -1) {
        document.write( 'Something went wrong setting the star\'s distance');
        } else {
        document.write( 'The star is now ' + someStar.getDis tance() + ' away');
        }

        By creating a Star object, you can encapsulate data validation (as in the method
        setDistance() and you manipulate the output in a single place (as with adding
        'au' to the value returned by getDistance()).

        --
        | Grant Wagner <gwagner@agrico reunited.com>

        * Client-side Javascript and Netscape 4 DOM Reference available at:
        *


        * Internet Explorer DOM Reference available at:
        *
        Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


        * Netscape 6/7 DOM Reference available at:
        * http://www.mozilla.org/docs/dom/domref/
        * Tips for upgrading JavaScript for Netscape 7 / Mozilla
        * http://www.mozilla.org/docs/web-deve...upgrade_2.html


        Comment

        • Jeff Thies

          #5
          Re: returning a list

          > If you know what you want to return, such as the description of a star, I
          would[color=blue]
          > strongly urge you to create an object Star, then return an instance of[/color]
          that[color=blue]
          > object. I don't know the properties of a star, but the code would look[/color]
          something[color=blue]
          > like:
          >
          > function Star(distance, brightness, temperature) {
          > this.distance = distance;
          > this.brightness = brightness;
          > this.temperatur e = temperature;
          >
          > this.setDistanc e = function(d) {
          > if (d > 1000000000) {
          > return -1;
          > } else {
          > this.distance = d;
          > return this.distance;
          > }
          > }
          > this.getDistanc e = function() {
          > return this.distance + 'au';
          > }
          > this.getBrightn ess = function() {
          > return this.brightness ;
          > }
          > this.getTempera ture = function() {
          > return this.temperatur e;
          > }
          > }[/color]

          I thought of making this more object oriented. Usually when I call the
          function I need 2 or 3 of the return values. These values just kind of fall
          out as the calculation proceeds. ie: var a may take 100% of the
          calculations, var b may take 80%. var c 98%. The calculations are extensive
          and I didn't want to redo them.

          Perhaps a wrapper around the function that turns it into an object???

          Below is the Moon function:

          function getMoon(date){
          var lo=64.975464;
          var Po=349.383063;
          var No=151.950429;
          var i=5.145396;

          var sun=(findSun(da te));
          var LAMDAsun=sun.LA MDAsun;
          var Msun=sun.M;
          var D=dayDiff(date) ;
          var l=(13.1763966*D )+lo;
          l=setRange(l);
          Mm=setRange(l - .1114041 * D - Po);
          var N=setRange(No - .0529539 * D);
          var C=l-LAMDAsun;
          var Ev=1.2739 * Math.sin(degree (2*C - Mm));
          var Ae=.1858 * Math.sin(degree (Msun));
          var A3=.37 * Math.sin(degree (Msun));
          var Mpm=Mm+Ev-Ae-A3;
          var Ec=6.2886 * Math.sin(degree (Mpm));
          var A4=.214 * Math.sin(degree (2* Mpm));
          var lp=l+Ev+Ec-Ae+A4;
          var V=.6583 * Math.sin(degree (lp-LAMDAsun)*2);
          var lpp=lp+V;

          var Np=N + (.16 * Math.sin(degree (LAMDAsun))); // test should be wrong

          var y=Math.sin(degr ee(lpp-Np)) * Math.cos(degree (i));
          var x=Math.cos(degr ee(lpp-Np));
          var arctan=Math.ata n(y/x)*180/Math.PI;

          // arctan amiguity test
          if((y>0)&&(x<0) ){arctan=arctan +180};
          if((y<0)&&(x<0) ){arctan=arctan +180};
          if((y<0)&&(x>0) ){arctan=arctan +360};

          var LAMDAm=arctan+N p;
          var Bm=Math.asin(Ma th.sin(degree(l pp-Np))*Math.sin(d egree(i)))*180/Math.PI;
          var equatorial=ecli pticToEquatoria l(LAMDAm,Bm);
          var Dm=lpp-LAMDAsun; // Dm is the phase of the moon in degrees ~13
          degrees/day
          var F=(1-Math.cos(degree (Dm)))/2;

          return{F:F,Dm:D m,alpha:equator ial.alpha,delta :equatorial.del ta,B:Bm,LAMDA:L A
          MDAm};
          }

          whew... Where do you draw the line between procedural, functional, object
          oriented and functional retrieve "hash objects"?

          maybe I should have gone to school after OO code was invented. No one ever
          asks me to write fortran.

          Cheers,
          Jeff


          [color=blue]
          >
          > Then in your function that returns information about a star, it could do
          > something like:
          >
          > return new Star(1, 2, 3);
          >
          > And the code that uses the returned value would be:
          >
          > var someStar = someFunctionTha tReturnsAStarOb ject();
          > document.write( 'The star is ' + someStar.getDis tance() + ' away');
          > if (someStar.setDi stance(10000000 0000) == -1) {
          > document.write( 'Something went wrong setting the star\'s distance');
          > } else {
          > document.write( 'The star is now ' + someStar.getDis tance() + ' away');
          > }
          >
          > By creating a Star object, you can encapsulate data validation (as in the[/color]
          method[color=blue]
          > setDistance() and you manipulate the output in a single place (as with[/color]
          adding[color=blue]
          > 'au' to the value returned by getDistance()).
          >
          > --
          > | Grant Wagner <gwagner@agrico reunited.com>
          >
          > * Client-side Javascript and Netscape 4 DOM Reference available at:
          > *
          >[/color]

          ames.html[color=blue]
          >
          > * Internet Explorer DOM Reference available at:
          > *
          >[/color]
          Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.

          try.asp[color=blue]
          >
          > * Netscape 6/7 DOM Reference available at:
          > * http://www.mozilla.org/docs/dom/domref/
          > * Tips for upgrading JavaScript for Netscape 7 / Mozilla
          > * http://www.mozilla.org/docs/web-deve...upgrade_2.html
          >
          >[/color]


          Comment

          Working...