Dis-Array??

R

Rich

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.
 
R

RobB

Rich said:
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/biblio?inkey=4-0072191279-1
Any help would be GREATLY appreciated.

Thank you,

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

Anthony Borla

Rich said:
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.fname + " "
+ my1DArray.lname + " : "
+ my1DArray.city);
}

=== */

// or (2)
for (var i in my1DArray)
{
for (var j in my1DArray)
{
WScript.Echo("[" + i + "][" + j + "] = "
+ my1DArray[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.length; j += 1)
WScript.Echo("[" + i + "][" + j + "] = "
+ my2DArray[j]);
}

showMy2DArray();
showMy1DArray();

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

More information may be obtained from the FAQ:

http://www.jibbering.com/faq/faq_notes/square_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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top