Pass variables between functions in Javascript?

I have functions that perform distance calculation with Google Maps.

They work smoothly, returning me the requested values.

This function returns a first value

  function calcDistance(response) {

    var origins = response.originAddresses;
    var destinations = response.destinationAddresses;


    for (var i = 0; i < origins.length; i++) {
     var results = response.rows[i].elements;

     for (var j = 0; j < results.length; j++) {
   
    distanzafissa = results[j].distance.value;
    distanzafissa = distanzafissa / 1000;
    
    distanzafissarr = Math.round(distanzafissa*100)/100;
    }
   }
  }

I would like the value of distanzafissarr to pass to another function so coded:

    function computeTotalDistance(result) {
      var total = 0;
      var myroute = result.routes[0];
      for (i = 0; i < myroute.legs.length; i++) {
        total += myroute.legs[i].distance.value;
      }
      total = total / 1000;
      
      
    var totalarr = Math.round(total*100)/100;
   }

I have tried to follow various answers on this site, but always getting errors.

104 thoughts on “Pass variables between functions in Javascript?”

  1. Pingback: keto taco bell
  2. certainly like your web site but you have to test the spelling on several of
    your posts. A number of them are rife with spelling problems
    and I find it very troublesome to inform the reality nevertheless
    I’ll definitely come again again.

    Reply
  3. Have you ever considered creating an ebook or guest authoring on other websites?

    I have a blog based upon on the same ideas you discuss and would really like
    to have you share some stories/information.
    I know my subscribers would value your work. If you’re even remotely interested, feel free to shoot me an e-mail.

    Reply
  4. Unquestionably believe that that you said. Your favourite
    reason seemed to be on the net the easiest
    thing to be mindful of. I say to you, I definitely
    get annoyed even as other people consider concerns that they plainly don’t recognize about.
    You controlled to hit the nail upon the highest as smartly as defined out the
    entire thing with no need side effect , other folks can take a signal.
    Will probably be back to get more. Thank you asmr https://app.gumroad.com/asmr2021/p/best-asmr-online asmr

    Reply
  5. Have you ever considered publishing an e-book or guest authoring on other blogs?
    I have a blog based on the same information you discuss and would really like to have you share some stories/information. I
    know my readers would enjoy your work. If you’re even remotely interested,
    feel free to shoot me an e-mail.

    Reply

Leave a Comment