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

Dis-Array??

P: n/a
Hello,

I'm "trying" to write a program that creates a multi-dimensional array. I
need to populate the array with values entered by the user.
I thought I could do something like this;
var fname = prompt ("Enter First Name:", " ");
var lname = prompt ("Enter Last Name:", " ");
var city = prompt ("Enter City you currently live in:", " ");

And then some how capture the info using something like this;

var myArray = new Array();
myArray[0] = new Array();
myArray[0][0] = "fname";
myArray[0][1] = "lname";
myArray[0][2] = "city";

But this did not work.

I am "new" and very frustrated that I don't have a clue how to make this
work. I'm going through the book "Beginning JavaScript" and I am just Not
Understanding what I need to do.
Any help would be GREATLY appreciated.

Thank you,
Rich

"If" you reply to me instead of Post to the group please remove the "No
Spam" from my email name.
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Rich wrote:
Hello,

I'm "trying" to write a program that creates a multi-dimensional array. I need to populate the array with values entered by the user.
Sounds like fun.
I thought I could do something like this;
var fname = prompt ("Enter First Name:", " ");
var lname = prompt ("Enter Last Name:", " ");
var city = prompt ("Enter City you currently live in:", " ");
You can.
And then some how capture the info using something like this;

var myArray = new Array();
Uh-huh, creates an (empty) array by calling the host (Array)
constructor. Could also use literal syntax:

var myArray = [];
myArray[0] = new Array();
OK, or myArray[0] = [];
myArray[0][0] = "fname";
myArray[0][1] = "lname";
myArray[0][2] = "city";

But this did not work.
Sure it did. If you go alert(myArray[0][0]) you'll see "fname". Exactly
what you put in there. Use the typeof operator - indispensable for this
sort of experimentation - and alert(typeof myArray[0][0]) will report
"string". Again, you did put a string in there, right?

Of course, if you wanted to put the value of the *variable* fname in
there....
myArray[0][0] = fname;
"fname" is 5 characters, and has no relationship to fname. The former
is string data, the latter a location in memory where data can be
stored.

Why not streamline a bit?

var myArray = [];
myArray[0] = [];
myArray[0][0] = prompt ("Enter First Name:", " ");
myArray[0][1] = prompt ("Enter Last Name:", " ");
myArray[0][2] = prompt ("Enter City you currently live in:", " ");

....eliminating the intermediate step of handing the values over to
three unnecessary variables. After all, that's what the array is for
(storing the data). You could also use an Array method:

.......
myArray[0].push(prompt ("Enter First Name:", " "));
.......

google 'javascript Array push' for an explanation. googling while
learning this stuff is more indispensable than typeof.

And alert(), alert(), alert()...much more productive than staring at
your display going...."huh?" #:=)
I am "new" and very frustrated that I don't have a clue how to make this work. I'm going through the book "Beginning JavaScript" and I am just Not Understanding what I need to do.
Throw "Beginning JavaScript" away.

http://www.powells.com/cgi-bin/bibli...4-0072191279-1
Any help would be GREATLY appreciated.

Thank you,


NP. Liked your topic (dis-...).

Jul 23 '05 #2

P: n/a

"Rich" <no************@verizon.net> wrote in message
news:nZt_d.8571$hA3.5203@trnddc09...
Hello,

I'm "trying" to write a program that creates a multi-dimensional
array. I need to populate the array with values entered by
the user.

I thought I could do something like this;

var fname = prompt ("Enter First Name:", " ");
var lname = prompt ("Enter Last Name:", " ");
var city = prompt ("Enter City you currently live in:", " ");

And then some how capture the info using something
like this;

var myArray = new Array();
myArray[0] = new Array();
myArray[0][0] = "fname";
myArray[0][1] = "lname";
myArray[0][2] = "city";

But this did not work.

By the looks of things it's not a 2D array that you are after, but rather, a
1D array of aggregate objects [yes, the distinctions *are* rather blurry
:)].

The following code [it's actually JScript executed under WSH but only minor
adjustments are needed for browser-based execution] should help you
understand the difference. Note that multiple ways of performing a
particular task are shown, marked (1) and (2), and one of them is always
commented out.

// ---------------------
function Address(fname, lname, city)
{
this.fname = fname; this.lname = lname;
this.city = city;

// *** Following line not strictly necessary
// as it defaults to this action
return this;
}

function showMy1DArray()
{
/* ===

// (1)
var my1DArray =
new Array({fname:"f1", lname:"l1", city:"c1"},
{fname:"f2", lname:"l2", city:"c2"});
=== */

// or (2)
var my1DArray =
new Array(new Address("f1", "l1", "c1"),
new Address("f2", "l2", "c2"));

/* ===

// (1)
for (var i in my1DArray)
{
WScript.Echo(my1DArray[i].fname + " "
+ my1DArray[i].lname + " : "
+ my1DArray[i].city);
}

=== */

// or (2)
for (var i in my1DArray)
{
for (var j in my1DArray[i])
{
WScript.Echo("[" + i + "][" + j + "] = "
+ my1DArray[i][j]);
}
}

}

function showMy2DArray()
{
/* ===

// (1)
var my2DArray =
new Array(new Array(), new Array());

my2DArray[0][0] = "a";
my2DArray[0][1] = "b";
my2DArray[0][2] = "c";

my2DArray[1][0] = "A";
my2DArray[1][1] = "B";
my2DArray[1][2] = "C";

=== */

// or (2)
var my2DArray =
new Array(new Array("a", "b", "c"),
new Array("A", "B", "C"));

for (var i = 0; i < my2DArray.length; i += 1)
for (var j = 0; j < my2DArray[i].length; j += 1)
WScript.Echo("[" + i + "][" + j + "] = "
+ my2DArray[i][j]);
}

showMy2DArray();
showMy1DArray();

// ---------------------

More information may be obtained from the FAQ:

http://www.jibbering.com/faq/faq_not..._brackets.html

or from Douglas Crockford's site:

http://www.crockford.com/

I am "new" and very frustrated that I don't have a clue how
to make this work. I'm going through the book
"Beginning JavaScript" and I am just Not Understanding what
I need to do.


Be patient, and keep at it - understanding will come with time.

I hope this helps.

Anthony Borla
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.