Date: 16/09/2015 22:11:57
From: JTQ
ID: 776600
Subject: JavaScript Help

Hi all

I’m working my way through the “JavaScript & AJAX for Dummies” book and I’ve created what should be a simple script for a 2-dimensional array, but I can’t get it working and have no idea what the problem is.

Hopefully someone can help fix it? Here’s what I’ve got:


<script type=“text/javascript”> cityName = new Array(“Indianapolis”,“New York”,“Tokyo”,“London”); distance = new Array( new Array(0,648,6476,4000), new array(648,0,6760,3470), new array(6476,6760,0,5956), new array(4000,3470,5956,0) );

function GetCity() { var theCity = “”; var cityMenu = “Please choose a city by typing in a number: \n\n”; cityMenu += “0) Indianapolis\n”; cityMenu += “1) New York\n”; cityMenu += “2) Tokyo\n”; cityMenu += “3) London\n”; theCity = prompt(cityMenu); return(theCity); } function main() { var output = “”; var from = GetCity(); var to = GetCity(); var result = distance; output = “The distance from “ + cityName; output += “ to “ + cityName; output += “ is “ + result + “ miles.”; alert (output); } main();

</script>

Reply Quote

Date: 16/09/2015 22:13:51
From: wookiemeister
ID: 776604
Subject: re: JavaScript Help

I don’t know java

whats it doing

whats it not doing

Reply Quote

Date: 16/09/2015 22:14:46
From: roughbarked
ID: 776605
Subject: re: JavaScript Help

wookiemeister said:


I don’t know java

whats it doing

whats it not doing

It isn’t java. It is javascript.

Reply Quote

Date: 16/09/2015 22:17:40
From: JTQ
ID: 776609
Subject: re: JavaScript Help

wookiemeister said:


I don’t know java

whats it doing

whats it not doing

When I run the script in a browser, it doesn’t do anything. No messages, no alerts, nothing. Just a blank page.

Reply Quote

Date: 16/09/2015 22:20:24
From: wookiemeister
ID: 776613
Subject: re: JavaScript Help

JTQ said:


wookiemeister said:

I don’t know java

whats it doing

whats it not doing

When I run the script in a browser, it doesn’t do anything. No messages, no alerts, nothing. Just a blank page.


what should it do when its running properly?

Reply Quote

Date: 16/09/2015 22:20:38
From: roughbarked
ID: 776614
Subject: re: JavaScript Help

var items = ,,];
alert(items); // 1

Reply Quote

Date: 16/09/2015 22:22:42
From: wookiemeister
ID: 776616
Subject: re: JavaScript Help

new array(4000,3470,5956,0)

have you missed a comma here?

Reply Quote

Date: 16/09/2015 22:22:48
From: sibeen
ID: 776617
Subject: re: JavaScript Help

wookiemeister said:


JTQ said:

wookiemeister said:

I don’t know java

whats it doing

whats it not doing

When I run the script in a browser, it doesn’t do anything. No messages, no alerts, nothing. Just a blank page.


what should it do when its running properly?

Ask for a city name via a number ( 0 – 3), then ask for another city name via a number (0 – 3), it then returns the distance between the two cities.

Reply Quote

Date: 16/09/2015 22:24:09
From: JTQ
ID: 776618
Subject: re: JavaScript Help

sibeen said:


wookiemeister said:

what should it do when its running properly?

Ask for a city name via a number ( 0 – 3), then ask for another city name via a number (0 – 3), it then returns the distance between the two cities.

Reply Quote

Date: 16/09/2015 22:24:37
From: wookiemeister
ID: 776619
Subject: re: JavaScript Help

I would write the main programme first up top and put the programmes it uses at the bottom

psychological order for me

Reply Quote

Date: 16/09/2015 22:25:22
From: roughbarked
ID: 776621
Subject: re: JavaScript Help

var items = ,,];

alert(items);

And the alert is reading from it,

To add things to it you would say items = “Label” ; items = “Value”;

If you want to do all the labels then all the values do..

for(var i = 0 ; i < labelssize; i ++)
{ items = labelhere;
}

for(var i = 0 ; i < labelssize; i ++)
{ items = valuehere;
}

Reply Quote

Date: 16/09/2015 22:28:17
From: wookiemeister
ID: 776623
Subject: re: JavaScript Help

assuming that its not a syntax error eg a missing semi colon, not naming a variable properly

you might want to look at the logic of the programming, maybe you’ve put the horse before the cart in part of the programme?

Reply Quote

Date: 16/09/2015 22:35:54
From: JTQ
ID: 776637
Subject: re: JavaScript Help

roughbarked said:


var items = ,,];

alert(items);

And the alert is reading from it,

To add things to it you would say items = “Label” ; items = “Value”;

If you want to do all the labels then all the values do..

for(var i = 0 ; i < labelssize; i ++)
{ items = labelhere;
}

for(var i = 0 ; i < labelssize; i ++)
{ items = valuehere;
}

I’m thinking but not sure how to update my coding to include your suggestion.

Reply Quote

Date: 17/09/2015 09:01:53
From: fsm
ID: 776748
Subject: re: JavaScript Help

JTQ said:


Hi all

I’m working my way through the “JavaScript & AJAX for Dummies” book and I’ve created what should be a simple script for a 2-dimensional array, but I can’t get it working and have no idea what the problem is.

Hopefully someone can help fix it? Here’s what I’ve got:


<script type=“text/javascript”> cityName = new Array(“Indianapolis”,“New York”,“Tokyo”,“London”); distance = new Array( new Array(0,648,6476,4000), new array(648,0,6760,3470), new array(6476,6760,0,5956), new array(4000,3470,5956,0) );

function GetCity() { var theCity = “”; var cityMenu = “Please choose a city by typing in a number: \n\n”; cityMenu += “0) Indianapolis\n”; cityMenu += “1) New York\n”; cityMenu += “2) Tokyo\n”; cityMenu += “3) London\n”; theCity = prompt(cityMenu); return(theCity); } function main() { var output = “”; var from = GetCity(); var to = GetCity(); var result = distance; output = “The distance from “ + cityName; output += “ to “ + cityName; output += “ is “ + result + “ miles.”; alert (output); } main();

</script>


Replace all of those dodgy quote marks and change the three examples of ‘new array’ to ‘new Array’ then it should work.

Reply Quote

Date: 18/09/2015 18:48:51
From: PM 2Ring
ID: 777407
Subject: re: JavaScript Help

Your code isn’t actually accessing the elements of your arrays.

I’ve simplified your code a bit: I’ve changed the array declarations to use simple literal arrays rather than using the Array() syntax. And I’ve added code to construct the menu prompt string from names in the cityName array appended to a passed in heading line. Also, I’ve embedded the script into a HTML page.

I couldn’t be hassled with trying to get it to display properly here, so I’ve put it on pastebin.

JTQ’s Distance Calculator

Reply Quote