Wrox Programmer Forums
|
BOOK: Beginning JavaScript
This is the forum to discuss the Wrox book Beginning JavaScript by Paul Wilton; ISBN: 9780764544057
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning JavaScript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 27th, 2004, 07:59 PM
Authorized User
 
Join Date: Nov 2003
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default Array

Just got to this code example and am confussed (again). Here is the code...

var inputName = "";
var namesArray = new Array();

while ( (inputName = prompt("Enter a name","")) != "")
{
   namesArray[namesArray.length] = inputName;
}
   namesArray.sort();

var namesList = namesArray.join("<BR>")
document.write(namesList);[/i]

I'm sure this is real basic to most, but I don't remember anywhere so far where we are shown how to populate array's with data from another variable or prompt - we have always populated the array ourselves so far that I can remember.

Does this mean that whatever the user enters in the prompt will be placed into the Array based on the.. what? ...length property - I guess meaning that it will be placed at the end of the array?

Confussed again...thanks folks.

 
Old January 28th, 2004, 04:20 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yes, correct. You can add elements to the array using this syntax:

myArray[indexer] = YourValue;

The length of the array indicates the number of items that are present in the array. This length is then used to put the new Names you get from the user in the array. The first name will be placed at namesArray[0], the second at [1] and so on. So, the names are added at the end of the array.

A the end, the array is sorted and its contents are written to the screen.

Does this help?

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old January 28th, 2004, 07:06 PM
Authorized User
 
Join Date: Nov 2003
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Imar. That helps. I totally understand the sort, join, concat, etc. methods of the array object but I didn't get the part about adding values to the array 'on the fly' so to speak. So anytime I want to take a value from a variable and place it in an array, I just use the .length method?

var yourName = "";
var friends = new Array();
yourName = prompt("Enter your name", "")
friends[friends.length] = yourName;

So that would store whatever was entered in the prompt in the friends array right?

I think I get it now if that is correct. Thanks a million sir !!!

 
Old January 29th, 2004, 04:10 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Well, it depends. The length property will return the current size of the array. Since the indexers of an array are zero based, but the length is one based, using the length will always put a new item at the end of the array. For example, consider your loop code:

Array has 0 elements. Length = 0. New item will be added at index 0 (the first item)
Array has 1 element. Length = 1. New item will be added at index 1 (the second item)

If you want to store elements at a specific location in the array, you can do that too (again, using the indexers). If you know your visitor has three friends, this code will ask their names and store them in an array with three elements (number 0, 1 and 2):
Code:
var yourName = "";
var friends = new Array();

yourName = prompt("Enter your name", "")
friends[0] = yourName;

yourName = prompt("Enter your name", "")
friends[1] = yourName;

yourName = prompt("Enter your name", "")
friends[2] = yourName;

var namesList = friends.join("<BR>")
document.write(namesList)
Cheers,

Imar



---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Convering a String Array to an Integer array nkrust C# 9 November 17th, 2010 12:02 PM
Go from 2d Array to 1d array without defining type OneQuestion General .NET 1 January 10th, 2008 11:13 AM
error when sorting an Array of Array nancy VBScript 2 February 17th, 2005 12:57 PM
Passing php array values to javascript array gkrishna Pro PHP 0 November 6th, 2004 03:20 AM
Array to Array comparison pavel Pro VB 6 0 March 24th, 2004 06:33 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.