Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 16th, 2004, 11:00 AM
Authorized User
 
Join Date: Aug 2003
Location: Nicholasville, KY, USA.
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default Add elements to array

As rudimentary as this is, it's got me stumped.

Using the Prompt method, a user is asked to repond to a question. Clicking OK, I want his answer to be added and stored in an array. Later, each element of the array is written out to a document.

While I can handle the array and writing out the elements of the array, I can seem to keep adding an element to an array. Everytime I run the function for adding an item, it starts over.

Does is the life of the array only as long as it takes to run the routine? Do I need to store the array in a global variable to retain the information added to the routine?

Any suggestions would be a help.

sabertec2
Reply With Quote
  #2 (permalink)  
Old February 16th, 2004, 11:13 AM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Try something like this:

Code:
var answers = new Array();
function question(num)
{
     if(num==1)answers[1] = prompt("What is the answer?"," ");
     else if(num==2)answers[2] = prompt("What is the answer?"," ");
     else if(num==3)answers[3] = prompt("What is the answer?"," ");
}

...
...

<a href="#" onclick="question(1)">Q1</a>
<a href="#" onclick="question(2)">Q2</a>
<a href="#" onclick="question(3)">Q3</a>
To add to an array:

Code:
your_array[your_array.length] = "Hi!";
HTH,

----------
---Snib---
----------
Reply With Quote
  #3 (permalink)  
Old February 16th, 2004, 03:35 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is a kind of silly use of if/else:

Quote:
quote:
function question(num)
{
     if(num==1)answers[1] = prompt("What is the answer?"," ");
     else if(num==2)answers[2] = prompt("What is the answer?"," ");
     else if(num==3)answers[3] = prompt("What is the answer?"," ");
}
Why not just do this and spare yourself the inefficiency of the if/else comparison?

function question(num)
{
     answers[num] = prompt("What is the answer?"," ");
}


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #4 (permalink)  
Old February 17th, 2004, 02:24 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Didn't think of that, Nik.

----------
---Snib---
----------
Reply With Quote
  #5 (permalink)  
Old February 17th, 2004, 03:03 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

=) Hey, I've done stuff like that more times than I care to admit. I remember one function I wrote where the last few lines were:

   if (var == true)
   {
       return true;
   }
   else
   {
       return false;
   }


When this should've sufficed:

   return var;


That was after about a 24-hour coding marathon I pulled several years ago.


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #6 (permalink)  
Old February 22nd, 2004, 03:16 PM
Authorized User
 
Join Date: Aug 2003
Location: Nicholasville, KY, USA.
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, all.

I've got it now. One problem I did have, however, was that the array was set to null or 0 everytime the function was called. Apparently, this was because I created the array within the function.

As the array is supposed to build as the user makes each selection, I realized from your examples that I had to place the array outside of the function and ended up making it a global variable.

Thank you,
sabertec2
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Working with array-type elements tclotworthy XSLT 2 October 11th, 2007 09:27 AM
Create XML file - add elements according to range remya1000 XML 5 October 3rd, 2007 02:50 PM
Accessing elements of an Array object abdulqadirpk VB How-To 1 April 2nd, 2007 11:13 AM
How do I add up values in an array? Lucy Classic ASP Basics 6 May 31st, 2005 05:26 AM



All times are GMT -4. The time now is 08:11 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.