Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

return undefined error 1

Status
Not open for further replies.

k4ghg

Technical User
Dec 25, 2001
191
US
Hi - I have been testing some code for using the results with Google maps. The calculations are fine, but when the value is returned I get an undefined error. Any help would be appreciated.

Also, my editor is highlighting the following line with an error:
yres = (ycalc[yi] / ydiv);

Txs. Ronnie


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"
<html xmlns="
<head>
<title></title>


<script type="text/javascript">

var lat = 28.48805;
var llong = -82.545564;
var longDir = "W";
var latDir = "N";
var stra = "";
var ychr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var ynum = "0123456789";
var yqth, yi, yk, ydiv, yres, ylp, y;
var y = 0;
var ycalc = new Array(0,0,0);
var yn = new Array(0,0,0,0,0,0,0);

stra += Math.round(lat * 10000) / 10000 + " " + latDir;
stra += ", ";
stra += Math.round(llong * 10000) / 10000 + " " + longDir;

ycalc[1] = llong + 180;
ycalc[2] = lat + 90;

for (yi = 1; yi < 3; ++yi) {
for (yk = 1; yk < 4; ++yk) {
if (yk != 3) {
if (yi == 1) {
if (yk == 1) ydiv = 20;
if (yk == 2) ydiv = 2;
}
if (yi == 2) {
if (yk == 1) ydiv = 10;
if (yk == 2) ydiv = 1;
}
yres = (ycalc[yi] / ydiv);
ycalc[yi] = yres;
if (ycalc[yi]>0)
ylp = Math.floor(yres)
else
ylp = Math.ceil(yres);
ycalc[yi] = (ycalc[yi] - ylp) * ydiv;
}
else {
if (yi == 1)
ydiv = 12
else
ydiv = 24;
yres = ycalc[yi] * ydiv;
ycalc[yi] = yres;
if (ycalc[yi]> 0)
ylp = Math.floor(yres)
else
ylp = Math.ceil(yres);
}

++y;
yn[y] = ylp;
}
}

yqth = ychr.charAt(yn[1]) + ychr.charAt(yn[4]) + ynum.charAt(yn[2]);
yqth += ynum.charAt(yn[5]) + ychr.charAt(yn[3])+ ychr.charAt(yn[6]);
stra += ", " + yqth;
alert(stra);
return stra;
}
</script>

</head>

<body>

<script type="text/javascript">
var stra;

test();

alert(stra);

</script>

</body>

</html>
 
I don't see a function called Test in what you have posted, but assume that it is the code in the script tags, so if you make that a function and change the rest of the page to the following you should be closer :

Code:
<script type="text/javascript">
    var stra=test();   
    alert(stra);   
</script>

Greg Griffiths
Livelink Certified Developer & ECM Global Star Champion 2005 & 2006
 
Hi Greg - Thank you that worked (the code was the function and when I copied it the function test() was deleted)!!! So, for future reference if I am going to call a function from a new <script>, I would need to declare the variable again as:

var something = functionname (something);

Thanks... Ronnie
 
Yes,
What you need to do is actually assign the return value of the function to a variable.

Variables don't automatically get created and values assigned to them.

The variable stra created outside is a completely different variable to the one inside your test() function. That is what is called variable scope.

Variables have a scope or area where they exist.
Your variable stra exists inside the function but only inside. The function then returns a value (the value that your variable stra has), not a variable, just a value.

You need to take that value and assign it to a variable. It doesn't automatically get assigned because you created a variable with the same name.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thanks Phil, that was very helpful... Ronnie
 
Glad I could help.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top