Array of Sunshine???

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

    Array of Sunshine???

    What my script does right now is :

    creates a multi-dimensional array, populates the array with values entered
    by the user. And then it Displays the result on an HTML page in a tabular
    format using the document.write( ) method.

    I then want it to Display the counts of strings and numbers below the table.

    I am trying to use the the NaN function to determine how many strings and
    numbers are entered in by the User and can not figure this out.

    I've been working on this for 5 days now and have not figured out what it is
    I need to do, or not to do.

    I am past frustrated and don't know where to find this information out. I
    got some help here before and am hoping to get some additonal help. I've
    been going through the book "Begining JavaScript - 2nd Edition" and I've
    read and reread various sections and I'm drawing a blank.

    I think I've looked at this script so long that I am just not seeing what I
    need to do.

    Any help, would be Greatly appreciated.

    THANKS,
    Richard


    <HTML>
    <HEAD>
    <TITLE>Array Man</TITLE>
    </HEAD>
    <BODY>
    <SCRIPT LANGUAGE = JavaScript>
    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:", " ");

    myArray[1] = [];
    myArray[1][0] = prompt ("Enter Zip Code:", " ");
    myArray[1][1] = prompt ("Enter your Age:", " ");
    myArray[1][2] = prompt ("Enter Number of Years in College:", " ");

    var row;
    var column;

    document.write( "<table width=\"25%\" border=\"1\" align=\"center\ "
    cellspacing=\"1 \" cellpadding=\"5 \" bgcolor=\"ddfff f\">");

    document.write( "<tr>");
    for (column in myArray[0])
    {
    document.write( "<td>" + myArray[0][column] + "</td>");
    }
    document.write( "</tr><tr>");
    for (column in myArray[1])
    {
    document.write( "<td>" + myArray[1][column] + "</td>");
    }
    document.write( "</tr>");
    document.write( "</table>");
    document.write( isNaN(parseInt (myArray[1])))
    </script>
    </body>
    </html>




  • mscir

    #2
    Re: Array of Sunshine???

    Rich wrote:
    [color=blue]
    > What my script does right now is :[/color]
    ....
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <HTML>
    <HEAD>
    <TITLE>Array Man</TITLE>
    </HEAD>
    <BODY>
    <script type = "text/javascript">
    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:", " ");

    myArray[1] = [];
    myArray[1][0] = prompt ("Enter Zip Code:", " ");
    myArray[1][1] = prompt ("Enter your Age:", " ");
    myArray[1][2] = prompt ("Enter Number of Years in College:", " ");

    document.write( "<table width=\"25%\" border=\"1\" align=\"center\ "
    cellspacing=\"1 \" cellpadding=\"5 \" bgcolor=\"ddfff f\">");
    document.write( "<tr>");
    for (var i=0;i<myArray[0].length;i++) {
    document.write( "<td>" + myArray[0][i] + "</td>");
    }
    document.write( "</tr><tr>");
    for (var j=0;j<myArray[1].length;j++) {
    document.write( "<td>" + myArray[1][j] + "</td>");
    }
    document.write( "</tr>");
    document.write( "</table>");
    document.write( isNaN(parseInt (myArray[1])))
    </script>
    </body>
    </html>

    Comment

    • RobG

      #3
      Re: Array of Sunshine???

      Rich wrote:[color=blue]
      > What my script does right now is :
      >
      > creates a multi-dimensional array, populates the array with values entered[/color]

      It creates an object that holds arrays.
      [color=blue]
      > by the user. And then it Displays the result on an HTML page in a tabular
      > format using the document.write( ) method.[/color]

      I think you'd be much better off to create the table as standard HTML
      then use an onload to get the required data - or even better, use a
      form within the page. A series of prompts is very, very annoying.

      If you use some other method, you can do much better validation as the
      user enters the data.

      There is validation of any data entry.

      [...][color=blue]
      >
      > document.write( "<table width=\"25%\" border=\"1\" align=\"center\ "
      > cellspacing=\"1 \" cellpadding=\"5 \" bgcolor=\"ddfff f\">");[/color]

      Allowing a line to wrap like this will always cause an error. Manually
      wrap your lines of code please.

      [...][color=blue]
      > document.write( isNaN(parseInt (myArray[1])))[/color]

      isNaN just returns true or false. How does this count numbers or
      words?

      Using parseInt this way means 123qweqw will be called a number. Is
      that OK?

      [...]

      Here is script which does what you want, but it is pretty awful - no
      data validation and only tests if 'numbers' are all digits. Otherwise,
      they are counted as words.

      Also, the HTML you are generating uses depreciated attributes, why not
      use a real HTML page and styles?


      <HTML><HEAD>
      <TITLE>Array Man</TITLE>
      </HEAD><BODY>
      <SCRIPT type="text/javascript">

      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:", " ");

      myArray[1] = [];
      myArray[1][0] = prompt ("Enter Zip Code:", " ");
      myArray[1][1] = prompt ("Enter your Age:", " ");
      myArray[1][2] = prompt ("Enter Number of Years in College:", " ");

      var row;
      var column;

      function countStuff(x) {
      var c='';
      var msg = '';
      // concatenate all the elements of x
      c = x.join(',');
      // Here is the number of characters (strip commas)
      msg += 'Number of characters: '
      + c.replace(/,/g,'').length;
      // Here is the number of words + numbers
      var words=0, nums=0;
      c = c.match(/\b\w+\b/g);
      for (var j=0, lex=c.length; j<lex; j++){
      (/\D+/.test(c[j]))? words++ : nums++;
      }
      msg += '<br>Number of words: ' + words;
      msg += '<br>Number of numbers: ' + nums;
      return msg;
      }

      document.write( "<table width=\"25%\" border=\"1\" align=\"center\ " "
      + " cellspacing=\"1 \" cellpadding=\"5 \" bgcolor=\"ddfff f\">");

      document.write( "<tr>");
      for (column in myArray[0]) {
      document.write( "<td>" + myArray[0][column] + "</td>");
      }
      document.write( "</tr><tr>");
      for (column in myArray[1]) {
      document.write( "<td>" + myArray[1][column] + "</td>");
      }
      document.write( "</tr>");
      document.write( "</table>");

      // Now call a function that actually counts
      // the words and numbers
      document.write( countStuff(myAr ray));
      </script>
      </body>
      </html>

      --
      Rob

      Comment

      Working...