473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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=\"ddffff\">");

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>


Jul 23 '05 #1
2 1545
Rich wrote:
What my script does right now is :

....
<!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=\"ddffff\">");
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>
Jul 23 '05 #2
Rich wrote:
What my script does right now is :

creates a multi-dimensional array, populates the array with values entered
It creates an object that holds arrays.
by the user. And then it Displays the result on an HTML page in a tabular
format using the document.write() method.
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.

[...]
document.write("<table width=\"25%\" border=\"1\" align=\"center\"
cellspacing=\"1\" cellpadding=\"5\" bgcolor=\"ddffff\">");
Allowing a line to wrap like this will always cause an error. Manually
wrap your lines of code please.

[...] document.write(isNaN(parseInt (myArray[1])))


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=\"ddffff\">");

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(myArray));
</script>
</body>
</html>

--
Rob
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
2
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
15
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
8
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
8
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
19
by: Henry | last post by:
I finally thought I had an understanding of multi dimensional arrays in C when I get this: #include <stdio.h> #define max_x 3 #define max_y 5 int array;
6
by: Gilles Ganault | last post by:
Hello I'm sure there's a much easier way to read a two-column, CSV file into an array, but I haven't found it in Google. Should I use the Array module instead? ========= a = i = 0
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.