1

I have the following code in my google map API:

var TC1image0 = "";
var TC1Intensity0 = "";
if (TC1wind0 < 41) { TC1Intensity0="Low Pressure Area"; TC1image0="lpagoogle.png"; }
else if (TC1wind0 < 63) { TC1Intensity0="Tropical Depression";  TC1image0="tdgoogle.png"; }
else if (TC1wind0 < 88) { TC1Intensity0="Tropical Storm"; TC1image0="tsgoogle.png";}
else if (TC1wind0 < 118) { TC1Intensity0="Severe Tropical Storm"; TC1image0="stsgoogle.png"; }
else if (TC1wind0 < 150) { TC1Intensity0="Typhoon"; TC1image0="tygoogle.png"; }
else if (TC1wind0 < 185) { TC1Intensity0="Severe Typhoon"; TC1image0="stygoogle.png"; }
else { TC1Intensity0="Super Typhoon"; TC1image0="sutygoogle.png"; }

Before I try to use function, I just repeat the codes every time when I need them, until TC1wind6 / TC1Intensity6 / TC1image6... It is completely a waste of space. So I try to modify the code using function:

function Intensity(TCwind, TCIntensity, TCimage) {
if (TCwind < 41) { TCIntensity="Low Pressure Area"; TCimage="lpagoogle.png"; }
else if (TCwind < 63) { TCIntensity="Tropical Depression";  TCimage="tdgoogle.png"; }
else if (TCwind < 88) { TCIntensity="Tropical Storm"; TCimage="tsgoogle.png";}
else if (TCwind < 118) { TCIntensity="Severe Tropical Storm"; TCimage="stsgoogle.png"; }
else if (TCwind < 150) { TCIntensity="Typhoon"; TCimage="tygoogle.png"; }
else if (TCwind < 185) { TCIntensity="Severe Typhoon"; TCimage="stygoogle.png"; }
else { TCIntensity="Super Typhoon"; TCimage="sutygoogle.png"; }
};

and I call the function like this:

var TC1wind0 = 65;
var TC1image0 = "";
var TC1Intensity0 = "";
Intensity(TC1wind0, TC1Intensity0, TC1image0);

The correct result should store "Tropical Storm" in TC1Intensity0 and "tsgoogle.png" in TC1image0. However it does not give correct result as the above "waste-of-space" method. Instead, the function seems not working. Both TC1Intensity0 and TC1image0 still give "". Why does this happen?

MrCode
  • 63,975
  • 10
  • 90
  • 112
hkson
  • 37
  • 6

2 Answers2

2

Since the three parameters you are passing are just strings, javascript will pass them by value. This means that making a change to TCwind inside the function will not change TCwind outside the function.

The most straight forward way to do that is to return the values in an object:

var result = Intensity(65, '', '');
var TCwind = result.TCwind;
var TCimage = result.TCimage;
var TCIntensity = result.TCIntensity;

function Intensity(TCwind, TCIntensity, TCimage) {
    /* snip */
    return {
        TCwind: TCwind,
        TCIntensity: TCIntensity,
        TCimage: TCimage
    };
};

For more information about pass by value / pass by reference, check out some of these other questions:

Community
  • 1
  • 1
jbabey
  • 45,965
  • 12
  • 71
  • 94
  • Thank you for your help. I have a further question, a similar one but should be more complicated: I have: `function TCDirection(PlaceLatlng, TCLatlng) { var TCDirectiontoHK = (google.maps.geometry.spherical.computeHeading (PlaceLatlng, TCLatlng)); if (TCDirectiontoHK<0) TCDirectiontoHK = Math.round(360+TCDirectiontoHK); else TCDirectiontoHK = Math.round(TCDirectiontoHK); };` and I want to call it like `TCDirection(HKLatlng, TC1Latlng0);` Of course it cannot work.. How can I get the bearing as expected? – hkson Jun 28 '13 at 19:30
  • @hkson you need to return something from the function and assign it to a variable. – jbabey Jun 28 '13 at 19:48
0

More simple would be, that you don't pass the vars as parameters. Than the function would write in the global vars.

function Intensity(TC1wind0){
[...]
}


var TC1wind0 = 65;
var TC1image0 = "";
var TC1Intensity0 = "";
Intensity(TC1wind0);
Michael Walter
  • 1,427
  • 12
  • 28