By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,496 Members | 1,266 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,496 IT Pros & Developers. It's quick & easy.

Convert array to string after "for loop" is finished

P: n/a
I am having problems getting values out of an array. The array is set
as a global array and values are pushed into it as they are read from
a JSON file using a "for loop". When the "for loop" is finished I want
to convert the array into a string which can be used by another
function. My attempt to do this is not working. The script looks like
this:

heights=[];
function getElevationInter(latv,lngv) {
var script = document.createElement('script');
document.body.appendChild(script);
script.src = 'http://ws.geonames.org/srtm3JSON?lat=' + latv +
'&lng=' + lngv + '&callback=loadJSON';
}

function loadJSON(result) {
heights.push(result.srtm3);
}

function getProfile() {
var dLat = lat[2]-lat[1];
var dLng = lng[2]-lng[1];
for (var i=0; i<d; i+=0.5) {
var latPoints = parseFloat((lat[1] + dLat*i/d)/10);
var lngPoints = parseFloat((lng[1] + dLng*i/d))/10;
getElevationInter(latPoints,lngPoints);
}
//alert(i)
if (i>d) {
alert(heights[2])
heightString=heights.toString()
alert(heightString)
}
}

Strangely, if I remove the comment // from the alert(i) the script
sometimes works but with the alert(i) commented out it never works.
The alert(heights[2] is there as a test, it is usually undefined
except sometimes when the alert(i) is uncommented when it returns the
correct value.

I would be grateful for any assistance.
Jun 27 '08 #1
Share this Question
Share on Google+
15 Replies


P: n/a
On Jun 22, 4:20 pm, Steve <stephen.jo...@googlemail.comwrote:
I am having problems getting values out of an array. The array is set
as a global array and values are pushed into it as they are read from
a JSON file using a "for loop". When the "for loop" is finished I want
to convert the array into a string which can be used by another
function. My attempt to do this is not working. The script looks like
this:

heights=[];
function getElevationInter(latv,lngv) {
var script = document.createElement('script');
document.body.appendChild(script);
script.src = 'http://ws.geonames.org/srtm3JSON?lat='+ latv +
'&lng=' + lngv + '&callback=loadJSON';

}

function loadJSON(result) {
heights.push(result.srtm3);

}

function getProfile() {
var dLat = lat[2]-lat[1];
var dLng = lng[2]-lng[1];
for (var i=0; i<d; i+=0.5) {
var latPoints = parseFloat((lat[1] + dLat*i/d)/10);
var lngPoints = parseFloat((lng[1] + dLng*i/d))/10;
getElevationInter(latPoints,lngPoints);
}
//alert(i)
if (i>d) {
alert(heights[2])
heightString=heights.toString()
alert(heightString)
}

}

Strangely, if I remove the comment // from the alert(i) the script
sometimes works but with the alert(i) commented out it never works.
The alert(heights[2] is there as a test, it is usually undefined
except sometimes when the alert(i) is uncommented when it returns the
correct value.

I would be grateful for any assistance.
Do you have a local variable in any function in your script 'http://
ws.geonames.org/srtm3JSON' named heights?

If so, try using the var keyword for your global above. 'var
heights=[]' instead of just 'heights=[]'.

The following paragraph was taken from here
http://developer.mozilla.org/en/docs...Statements:var

Using var outside a function is optional; assigning a value to an
undeclared variable implicitly declares it as a global variable.
However, it is recommended to always use var, and it is necessary
within functions in the following situations:

* If a variable in a scope containing the function (including the
global scope) has the same name.
* If recursive or multiple functions use variables with the same
name and intend those variables to be local.

Failure to declare the variable in these cases will very likely lead
to unexpected results.
Jun 27 '08 #2

P: n/a
On Jun 22, 4:20 pm, Steve <stephen.jo...@googlemail.comwrote:
I am having problems getting values out of an array. The array is set
as a global array and values are pushed into it as they are read from
a JSON file using a "for loop". When the "for loop" is finished I want
to convert the array into a string which can be used by another
function. My attempt to do this is not working. The script looks like
this:

heights=[];
function getElevationInter(latv,lngv) {
var script = document.createElement('script');
document.body.appendChild(script);
script.src = 'http://ws.geonames.org/srtm3JSON?lat='+ latv +
'&lng=' + lngv + '&callback=loadJSON';

}

function loadJSON(result) {
heights.push(result.srtm3);

}

function getProfile() {
var dLat = lat[2]-lat[1];
var dLng = lng[2]-lng[1];
for (var i=0; i<d; i+=0.5) {
var latPoints = parseFloat((lat[1] + dLat*i/d)/10);
var lngPoints = parseFloat((lng[1] + dLng*i/d))/10;
getElevationInter(latPoints,lngPoints);
}
//alert(i)
if (i>d) {
alert(heights[2])
heightString=heights.toString()
alert(heightString)
}

}

Strangely, if I remove the comment // from the alert(i) the script
sometimes works but with the alert(i) commented out it never works.
The alert(heights[2] is there as a test, it is usually undefined
except sometimes when the alert(i) is uncommented when it returns the
correct value.

I would be grateful for any assistance.
Possibly a scope problem with 'heights=[];' ?

Jun 27 '08 #3

P: n/a
On Jun 23, 7:20 am, Steve <stephen.jo...@googlemail.comwrote:
I am having problems getting values out of an array. The array is set
as a global array and values are pushed into it as they are read from
a JSON file using a "for loop". When the "for loop" is finished I want
to convert the array into a string which can be used by another
function. My attempt to do this is not working. The script looks like
this:

heights=[];
You should always declare variables with var, although that may not be
an issue in this case.

var heights=[];

function getElevationInter(latv,lngv) {
var script = document.createElement('script');
document.body.appendChild(script);
script.src = 'http://ws.geonames.org/srtm3JSON?lat='+ latv +
'&lng=' + lngv + '&callback=loadJSON';

}

function loadJSON(result) {
heights.push(result.srtm3);

}

function getProfile() {
var dLat = lat[2]-lat[1];
What is the result of that? Is it a number? See below.

var dLng = lng[2]-lng[1];
for (var i=0; i<d; i+=0.5) {
var latPoints = parseFloat((lat[1] + dLat*i/d)/10);
var lngPoints = parseFloat((lng[1] + dLng*i/d))/10;
There seems to be a misplaced bracket in the above, -----^

Why have you used parseFloat? What are the values in the lat and lng
arrays? If they are numbers, there is no need for parseFloat. If
they are strings, you need to use parseFloat on the individual values,
e.g.

var latPoints = (parseFloat(lat[1]) + dLat*i/d)/10);

The use of multiplication and division will automatcially convert
dLat, i and d to numbers if they are strings (provided they are
strings that can be converted to numbers, like 23.45).

getElevationInter(latPoints,lngPoints);
Here you load the data, but do you wait for the element to become
available?

}
//alert(i)
if (i>d) {
alert(heights[2])
heightString=heights.toString()
alert(heightString)
}

}

Strangely, if I remove the comment // from the alert(i) the script
sometimes works but with the alert(i) commented out it never works.
The alert(heights[2] is there as a test, it is usually undefined
except sometimes when the alert(i) is uncommented when it returns the
correct value.
That seems like a timing issue. Are you wating for the script element
to exist before trying to use the content? Try puting the statements
after the alter into a separate function and call it after a delay of
say 1000ms or so (or maybe 2000ms to be sure).
--
Rob
Jun 27 '08 #4

P: n/a
On Jun 23, 3:54 am, Doug Gunnoe <douggun...@gmail.comwrote:
On Jun 22, 4:20 pm, Steve <stephen.jo...@googlemail.comwrote:
I am having problems getting values out of an array. The array is set
as a global array

Possibly a scope problem with 'heights=[];' ?
The scope of heights=[] is Global.

Steve.

Jun 27 '08 #5

P: n/a
Thanks for the reply but.........

On Jun 23, 5:08 am, RobG <rg...@iinet.net.auwrote:
>
var dLng = lng[2]-lng[1];
for (var i=0; i<d; i+=0.5) {
var latPoints = parseFloat((lat[1] + dLat*i/d)/10);
var lngPoints = parseFloat((lng[1] + dLng*i/d))/10;

There seems to be a misplaced bracket in the above, -----^
You're right, I'll correct this when I get home tonight.
>
Why have you used parseFloat? What are the values in the lat and lng
arrays? If they are numbers, there is no need for parseFloat. If
they are strings, you need to use parseFloat on the individual values,
e.g.

var latPoints = (parseFloat(lat[1]) + dLat*i/d)/10);

The use of multiplication and division will automatcially convert
dLat, i and d to numbers if they are strings (provided they are
strings that can be converted to numbers, like 23.45).
I used parsefloat because of the problems I was having. I am aware
that is not necessary but it shouldn't matter or cause a problem
should it?
>
getElevationInter(latPoints,lngPoints);

Here you load the data, but do you wait for the element to become
available?
}
//alert(i)
if (i>d) {
alert(heights[2])
heightString=heights.toString()
alert(heightString)
}
}
Yes, because if I put an alert(heights) in the loadJSON function like
below the result values are displaed in the alert.

function loadJSON(result) {
heights.push(result.srtm3);
alert(heights)
}

Strangely, if I remove the comment // from the alert(i) the script
sometimes works but with the alert(i) commented out it never works.
The alert(heights[2] is there as a test, it is usually undefined
except sometimes when the alert(i) is uncommented when it returns the
correct value.

That seems like a timing issue. Are you wating for the script element
to exist before trying to use the content? Try puting the statements
after the alter into a separate function and call it after a delay of
say 1000ms or so (or maybe 2000ms to be sure).
Maybe, but why should a delay be necessary as I know that the values
are in the array, see above comments on loadJSON.
Jun 27 '08 #6

P: n/a
On Jun 23, 4:58 pm, Steve <stephen.jo...@googlemail.comwrote:
Thanks for the reply but.........

On Jun 23, 5:08 am, RobG <rg...@iinet.net.auwrote:
var dLng = lng[2]-lng[1];
for (var i=0; i<d; i+=0.5) {
var latPoints = parseFloat((lat[1] + dLat*i/d)/10);
var lngPoints = parseFloat((lng[1] + dLng*i/d))/10;
There seems to be a misplaced bracket in the above, -----^

You're right, I'll correct this when I get home tonight.
Why have you used parseFloat? What are the values in the lat and lng
arrays? If they are numbers, there is no need for parseFloat. If
they are strings, you need to use parseFloat on the individual values,
e.g.
var latPoints = (parseFloat(lat[1]) + dLat*i/d)/10);
The use of multiplication and division will automatcially convert
dLat, i and d to numbers if they are strings (provided they are
strings that can be converted to numbers, like 23.45).

I used parsefloat because of the problems I was having. I am aware
that is not necessary but it shouldn't matter or cause a problem
should it?
No, I was just seeing if you expect it to do anything that it might
not be doing.

getElevationInter(latPoints,lngPoints);
Here you load the data, but do you wait for the element to become
available?
}
//alert(i)
if (i>d) {
alert(heights[2])
heightString=heights.toString()
alert(heightString)
}
}

Yes, because if I put an alert(heights) in the loadJSON function like
below the result values are displaed in the alert.

function loadJSON(result) {
heights.push(result.srtm3);
alert(heights)

}
The code you posted doesn't call loadJSON, it is included in the URI
for the script element's src attribute. Where do you call it from?
Strangely, if I remove the comment // from the alert(i) the script
sometimes works but with the alert(i) commented out it never works.
The alert(heights[2] is there as a test, it is usually undefined
except sometimes when the alert(i) is uncommented when it returns the
correct value.
That seems like a timing issue. Are you wating for the script element
to exist before trying to use the content? Try puting the statements
after the alter into a separate function and call it after a delay of
say 1000ms or so (or maybe 2000ms to be sure).

Maybe, but why should a delay be necessary as I know that the values
are in the array, see above comments on loadJSON.
Did you try it?

The reason I think it might be an issue is that after you assign a
value to the DOM element's src property, the function moves on. I
don't think there is anything that says it should wait until the
script file has actually finished loading and code executed before
moving to the next step in the function.
--
Rob
Jun 27 '08 #7

P: n/a
On Jun 23, 9:11 am, RobG <rg...@iinet.net.auwrote:
On Jun 23, 4:58 pm, Steve <stephen.jo...@googlemail.comwrote:
Thanks for the reply but.........
On Jun 23, 5:08 am, RobG <rg...@iinet.net.auwrote:
var dLng = lng[2]-lng[1];
for (var i=0; i<d; i+=0.5) {
var latPoints = parseFloat((lat[1] + dLat*i/d)/10);
var lngPoints = parseFloat((lng[1] + dLng*i/d))/10;
There seems to be a misplaced bracket in the above, -----^
You're right, I'll correct this when I get home tonight.
Why have you used parseFloat? What are the values in the lat and lng
arrays? If they are numbers, there is no need for parseFloat. If
they are strings, you need to use parseFloat on the individual values,
e.g.
var latPoints = (parseFloat(lat[1]) + dLat*i/d)/10);
The use of multiplication and division will automatcially convert
dLat, i and d to numbers if they are strings (provided they are
strings that can be converted to numbers, like 23.45).
I used parsefloat because of the problems I was having. I am aware
that is not necessary but it shouldn't matter or cause a problem
should it?

No, I was just seeing if you expect it to do anything that it might
not be doing.
getElevationInter(latPoints,lngPoints);
Here you load the data, but do you wait for the element to become
available?
}
//alert(i)
if (i>d) {
alert(heights[2])
heightString=heights.toString()
alert(heightString)
}
}
Yes, because if I put an alert(heights) in the loadJSON function like
below the result values are displaed in the alert.
function loadJSON(result) {
heights.push(result.srtm3);
alert(heights)
}

The code you posted doesn't call loadJSON, it is included in the URI
for the script element's src attribute. Where do you call it from?
Strangely, if I remove the comment // from the alert(i) the script
sometimes works but with the alert(i) commented out it never works.
The alert(heights[2] is there as a test, it is usually undefined
except sometimes when the alert(i) is uncommented when it returns the
correct value.
That seems like a timing issue. Are you wating for the script element
to exist before trying to use the content? Try puting the statements
after the alter into a separate function and call it after a delay of
say 1000ms or so (or maybe 2000ms to be sure).
Maybe, but why should a delay be necessary as I know that the values
are in the array, see above comments on loadJSON.

Did you try it?

The reason I think it might be an issue is that after you assign a
value to the DOM element's src property, the function moves on. I
don't think there is anything that says it should wait until the
script file has actually finished loading and code executed before
moving to the next step in the function.

--
Rob
Hi Rob,

I will try it this evening and let you know what happens.

Thanks for your help.

Steve.
Jun 27 '08 #8

P: n/a
On Jun 23, 1:45*am, Steve <stephen.jo...@googlemail.comwrote:
On Jun 23, 3:54 am, Doug Gunnoe <douggun...@gmail.comwrote:
On Jun 22, 4:20 pm, Steve <stephen.jo...@googlemail.comwrote:
I am having problems getting values out of an array. The array is set
as a global array
Possibly a scope problem with 'heights=[];' ?

The scope of heights=[] is Global.

Steve.
Yes I know.

My first inclination was that there could have been some issues
regarding this.

from http://developer.mozilla.org/en/docs...Statements:var
--
Using var outside a function is optional; assigning a value to an
undeclared variable implicitly declares it as a global variable.
However, it is recommended to always use var, and it is necessary
within functions in the following situations:

* If a variable in a scope containing the function (including the
global scope) has the same name.
* If recursive or multiple functions use variables with the same
name and intend those variables to be local.

Failure to declare the variable in these cases will very likely lead
to unexpected results.
--

But, after testing the idea out for a little while I decided that this
probably wasn't the problem.

I am intrigued by RobG's thought that this could be a timing problem.
And I too would like to know at what point you are calling functions
in the the script you insert into the DOM tree.

Good luck with this. Sorry I wasn't any help.

Jun 27 '08 #9

P: n/a
I remember having this problem when trying to use google maps. I'm
raking my brains to remeber what the soloution was
Jun 27 '08 #10

P: n/a
On Jun 23, 9:11 am, RobG <rg...@iinet.net.auwrote:
On Jun 23, 4:58 pm, Steve <stephen.jo...@googlemail.comwrote:
Thanks for the reply but.........
On Jun 23, 5:08 am, RobG <rg...@iinet.net.auwrote:
var dLng = lng[2]-lng[1];
for (var i=0; i<d; i+=0.5) {
var latPoints = parseFloat((lat[1] + dLat*i/d)/10);
var lngPoints = parseFloat((lng[1] + dLng*i/d))/10;
There seems to be a misplaced bracket in the above, -----^
You're right, I'll correct this when I get home tonight.
Why have you used parseFloat? What are the values in the lat and lng
arrays? If they are numbers, there is no need for parseFloat. If
they are strings, you need to use parseFloat on the individual values,
e.g.
var latPoints = (parseFloat(lat[1]) + dLat*i/d)/10);
The use of multiplication and division will automatcially convert
dLat, i and d to numbers if they are strings (provided they are
strings that can be converted to numbers, like 23.45).
I used parsefloat because of the problems I was having. I am aware
that is not necessary but it shouldn't matter or cause a problem
should it?

No, I was just seeing if you expect it to do anything that it might
not be doing.
getElevationInter(latPoints,lngPoints);
Here you load the data, but do you wait for the element to become
available?
}
//alert(i)
if (i>d) {
alert(heights[2])
heightString=heights.toString()
alert(heightString)
}
}
Yes, because if I put an alert(heights) in the loadJSON function like
below the result values are displaed in the alert.
function loadJSON(result) {
heights.push(result.srtm3);
alert(heights)
}

The code you posted doesn't call loadJSON, it is included in the URI
for the script element's src attribute. Where do you call it from?
Strangely, if I remove the comment // from the alert(i) the script
sometimes works but with the alert(i) commented out it never works.
The alert(heights[2] is there as a test, it is usually undefined
except sometimes when the alert(i) is uncommented when it returns the
correct value.
That seems like a timing issue. Are you wating for the script element
to exist before trying to use the content? Try puting the statements
after the alter into a separate function and call it after a delay of
say 1000ms or so (or maybe 2000ms to be sure).
Maybe, but why should a delay be necessary as I know that the values
are in the array, see above comments on loadJSON.

Did you try it?

The reason I think it might be an issue is that after you assign a
value to the DOM element's src property, the function moves on. I
don't think there is anything that says it should wait until the
script file has actually finished loading and code executed before
moving to the next step in the function.
Brilliant! That solved the problem. Just when you start to think that
you know JavaScript something like this happens ;-)
Thanks to all who replied and just to show how it was done here is the
altered script:-

var heights=[];
function getElevationInter(latv,lngv) {
var script = document.createElement('script');
document.body.appendChild(script);
script.src = 'http://ws.geonames.org/srtm3JSON?lat=' + latv +
'&lng=' + lngv + '&callback=loadJSON';
}

function loadJSON(result) {
heights.push(result.srtm3)
}

function getProfile() {
var dLat = lat[2]-lat[1];
var dLng = lng[2]-lng[1];
for (var i=0; i<d; i+=0.5) {
var latPoints = parseFloat((lat[1] + dLat*i/d)/10);
var lngPoints = parseFloat((lng[1] + dLng*i/d)/10);
getElevationInter(latPoints,lngPoints);
}
setTimeout(heightToString,1000)
}

function heightToString() {
alert(heights[2]);
heightString=heights.toString();
alert(heightString);
}
Thanks again,

Steve.

Jun 27 '08 #11

P: n/a
In comp.lang.javascript message <4f99b1b0-1853-4dd5-b1f4-b93a3f998a75@x3
5g2000hsb.googlegroups.com>, Sun, 22 Jun 2008 14:20:23, Steve
<st***********@googlemail.composted:
>I am having problems getting values out of an array. The array is set
as a global array and values are pushed into it as they are read from
a JSON file using a "for loop". When the "for loop" is finished I want
to convert the array into a string which can be used by another
function.

When something peculiar is seen, state which browser you are using; it
might matter.

The FOR loop is counting in half-integers. If d is half-integer, it
leaves i==d, otherwise i>d. That might be relevant.

The alert will cause a delay, which might matter. Consider trying,
instead, alert(heights).

ISTM that your FOR loop invokes geonames a number of times in rapid
sequence; there may be no guarantee that the responses come in the same
sequence, which might matter.

Instead of using a FOR loop, you could make the first geonames call and
use the callback function to make, or schedule, the next call, if not
finished.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Jun 27 '08 #12

P: n/a
On Jun 23, 11:29*am, Steve <stephen.jo...@googlemail.comwrote:
* * setTimeout(heightToString,1000)

Good call Rob.

Jun 27 '08 #13

P: n/a
On Jun 23, 1:02 pm, Dr J R Stockton <j...@merlyn.demon.co.ukwrote:
Instead of using a FOR loop, you could make the first geonames call and
use the callback function to make, or schedule, the next call, if not
finished.
As you will have seen the problem has been solved but I am intrigued
by your suggestion to use call and callback functions as I am not
aware of how these functions work. Could you give an example please?

Regards, Steve.
Jun 27 '08 #14

P: n/a
In comp.lang.javascript message <d551d2b3-2a42-4eae-afdd-12ba7adbf445@a7
0g2000hsh.googlegroups.com>, Tue, 24 Jun 2008 00:58:03, Steve
<st***********@googlemail.composted:
>On Jun 23, 1:02 pm, Dr J R Stockton <j...@merlyn.demon.co.ukwrote:
>Instead of using a FOR loop, you could make the first geonames call and
use the callback function to make, or schedule, the next call, if not
finished.
As you will have seen the problem has been solved but I am intrigued
by your suggestion to use call and callback functions as I am not
aware of how these functions work. Could you give an example please?
You are already using one, if I have the terminology correct. Indeed,
your code includes '&callback=loadJSON'; .

Make the content of your loop a function; instead of the loop, call it
once, with the first datum. When the result appears, loadJSON will be
called; at the end of that routine, if there are more data points to be
processed, call the function again, otherwise call the terminal
processing of heights.

Using a timeout within the loop, you are navigating between the Scylla
of wasting time and the Charybdis of not always waiting long enough -
remember that the response-time of geonames is likely yo vary
considerably and unpredictably. But, since it is possible that geonames
may not be working, I suggest an overall timeout calling a prompt for
choosing between waiting again and abandoning all hope.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)
Jun 27 '08 #15

P: n/a
On Jun 24, 8:28 pm, Dr J R Stockton <j...@merlyn.demon.co.ukwrote:
Make the content of your loop a function; instead of the loop, call it
once, with the first datum. When the result appears, loadJSON will be
called; at the end of that routine, if there are more data points to be
processed, call the function again, otherwise call the terminal
processing of heights.

Using a timeout within the loop, you are navigating between the Scylla
of wasting time and the Charybdis of not always waiting long enough -
remember that the response-time of geonames is likely yo vary
considerably and unpredictably. But, since it is possible that geonames
may not be working, I suggest an overall timeout calling a prompt for
choosing between waiting again and abandoning all hope.
Thanks, that makes a lot of sense.

Steve.
Jun 27 '08 #16

This discussion thread is closed

Replies have been disabled for this discussion.