 |
| 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 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
|
|
|
|

February 16th, 2004, 11:00 AM
|
|
Authorized User
|
|
Join Date: Aug 2003
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

February 16th, 2004, 11:13 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
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---
----------
|
|

February 16th, 2004, 03:35 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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/
|
|

February 17th, 2004, 02:24 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Didn't think of that, Nik. 
----------
---Snib---
----------
|
|

February 17th, 2004, 03:03 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
=) 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/
|
|

February 22nd, 2004, 03:16 PM
|
|
Authorized User
|
|
Join Date: Aug 2003
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|
 |