Dis-Array??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rich

    Dis-Array??

    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.


  • RobB

    #2
    Re: Dis-Array??

    Rich wrote:[color=blue]
    > Hello,
    >
    > I'm "trying" to write a program that creates a multi-dimensional[/color]
    array. I[color=blue]
    > need to populate the array with values entered by the user.[/color]

    Sounds like fun.
    [color=blue]
    > 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:", " ");[/color]

    You can.
    [color=blue]
    > And then some how capture the info using something like this;
    >
    > var myArray = new Array();[/color]

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

    var myArray = [];
    [color=blue]
    > myArray[0] = new Array();[/color]

    OK, or myArray[0] = [];
    [color=blue]
    > myArray[0][0] = "fname";
    > myArray[0][1] = "lname";
    > myArray[0][2] = "city";
    >
    > But this did not work.[/color]

    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....
    [color=blue]
    > myArray[0][0] = fname;[/color]

    "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?" #:=)
    [color=blue]
    > I am "new" and very frustrated that I don't have a clue how to make[/color]
    this[color=blue]
    > work. I'm going through the book "Beginning JavaScript" and I am[/color]
    just Not[color=blue]
    > Understanding what I need to do.[/color]

    Throw "Beginning JavaScript" away.


    [color=blue]
    > Any help would be GREATLY appreciated.
    >
    > Thank you,[/color]

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

    Comment

    • Anthony Borla

      #3
      Re: Dis-Array??


      "Rich" <nospampetraman @verizon.net> wrote in message
      news:nZt_d.8571 $hA3.5203@trndd c09...[color=blue]
      > 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.
      >[/color]

      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:"f 1", 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(my 1DArray[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.lengt h; 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:



      or from Douglas Crockford's site:


      [color=blue]
      >
      > 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.
      >[/color]

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

      I hope this helps.

      Anthony Borla


      Comment

      Working...