/    Sign up×
Community /Pin to ProfileBookmark

Seperating Elements in Arrays

? Hi everybody, I am presently trying to learn javascript and have been reading sams teachyourself javas… in 21 days. I have been set the task to seperate out the names of one Array with the relevent ages of another Array. I have used a combination of the pop() and push() methods together with a while loop to seperate an array of ages into ages belonging to adults and ages belonging to minors. I created another array called names in parallel with the array called ages and another two arrays to hold the adult names and the minor names.I was told i will need to add some code to a for loop that will seperate out the names with the relevent ages.If anybody could provide code it would be much appreciated. Thanks
Ross

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@ExuroJun 12.2004 — Couldn't you just pop the corresponding [FONT=courier new]name[/FONT] for each [FONT=courier new]age[/FONT] in your current loop? Then you can place each one in their proper arrays at the same time as well...
Copy linkTweet thisAlerts:
@neil9999Jun 12.2004 — I'm not sure what you want, but see if this is it:

[code=php]<body>
<script type="text/javascript">
//In array, put the first person's name, then their age, then the second person's name, then their age etc.
var people=["Bill",21,"Ben",4,"Fred",15,"Bob",60]
//set the value of var adultage to the age at which a minor becomes an adult.
var adultage=18;
var adults=new Array();
var minors=new Array();
var start=0;
while(start<people.length){
if(people[start+1]<adultage){
minors[minors.length]=people[start];
minors[minors.length]=people[start+1]
}
else{
adults[adults.length]=people[start];
adults[adults.length]=people[start+1]
}
start+=2;
}
alert(minors);
alert(adults);
</script>
</body>[/code]


The minors and their ages are stored to an array called minors, and the adults to an array called adults.

Hope that helps,

Neil
Copy linkTweet thisAlerts:
@rpmayauthorJun 14.2004 — Thanks for your help Neil. Exuro would you be able to show me what you mean?

Ross
Copy linkTweet thisAlerts:
@ExuroJun 14.2004 — I was pretty much talking about what neil posted.
Copy linkTweet thisAlerts:
@rpmayauthorJun 15.2004 — Cheers for your help guy's.


Ross
×

Success!

Help @rpmay spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...