/    Sign up×
Community /Pin to ProfileBookmark

Parallel Arrays

Hi

I have the following code, and want to express the max points result in parallel with the winners names (i.e the two couples who got 7), What do i need to do to amend this code at the bottom?

[CODE]var contestantNamesArray = [‘bob and pete’, ‘tim and jon’, ‘mick and alun’, ‘van and brad’, ‘chez and jaz’];

var judgesPointsArray = [2,1,5,4,3];
var audiencePointsArray = [4,5,2,3,1];

var combinedPointsArray = new Array (5);

for (var points = 0; points < judgesPointsArray.length; points++)
{
combinedPointsArray[points] = judgesPointsArray[points] + audiencePointsArray[points];
}

var maxPoints = combinedPointsArray[0]

for (var points = 0; points < judgesPointsArray.length; points++)
{
if (combinedPointsArray[points] > maxPoints)
{
maxPoints = combinedPointsArray[points];
}
}
document.write(‘The maximum number of points was ‘ + maxPoints + ‘<BR>’)

document.write(‘The couple(s) scoring the maximum were:’ + ‘<BR>’)

[U]{
document.write(contestantNamesArray[maxPoints] );
}
[/U]

[/CODE]

Thanks ?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@FangMay 15.2010 — &lt;script type="text/javascript"&gt;
var contestantNamesArray = ['bob and pete', 'tim and jon', 'mick and alun', 'van and brad', 'chez and jaz'];
var judgesPointsArray = [2,1,5,4,3];
var audiencePointsArray = [4,5,2,3,1];
var combinedPointsArray = new Array (5);

for (var points = 0; points &lt; judgesPointsArray.length; points++)
{
combinedPointsArray[points] = judgesPointsArray[points] + audiencePointsArray[points];
}

var maxPoints = combinedPointsArray[0];
var winners = [];
for (var points = 0; points &lt; judgesPointsArray.length; points++)
{
if (combinedPointsArray[points] &gt;= maxPoints)
{
if(combinedPointsArray[points] &gt; maxPoints) {winners.length = 0;} // empty array, new max points
winners.push(points); // save the winner
maxPoints = combinedPointsArray[points];
}
}
document.write('The maximum number of points was ' + maxPoints + '&lt;BR&gt;')
document.write('The couple(s) scoring the maximum were:' + '&lt;BR&gt;')

for(var i=0; i&lt;winners.length; i++) {
document.write(contestantNamesArray[winners[i]], '&lt;br&gt;' );
}
&lt;/script&gt;
Copy linkTweet thisAlerts:
@AdytyperauthorMay 15.2010 — Thanks, it's not quite what i'm after doing.

I dont want to assign another variable, i.e "winners", so is there a way of doing it?
Copy linkTweet thisAlerts:
@FangMay 16.2010 — Not if there is a possibility of multiple winners.

You could combine the first 2 [I]for [/I]loops:if ( (judgesPointsArray[points] + audiencePointsArray[points]) &gt;= maxPoints)
Copy linkTweet thisAlerts:
@AdytyperauthorMay 16.2010 — So where could i add this loop for it to work?
Copy linkTweet thisAlerts:
@FangMay 16.2010 — &lt;script type="text/javascript"&gt;
var contestantNamesArray = ['bob and pete', 'tim and jon', 'mick and alun', 'van and brad', 'chez and jaz'];
var judgesPointsArray = [2,1,5,4,3];
var audiencePointsArray = [4,5,2,3,1];

var maxPoints = 0;
var winners = [];
for (var points = 0; points &lt; judgesPointsArray.length; points++)
{
var temp = judgesPointsArray[points] + audiencePointsArray[points];
if (temp &gt;= maxPoints)
{
if(temp &gt; maxPoints) {winners.length = 0;} // empty array, new max points
winners.push(points); // save the winner
maxPoints = temp;
}
}
document.write('The maximum number of points was ' + maxPoints + '&lt;BR&gt;')
document.write('The couple(s) scoring the maximum were:&lt;BR&gt;')

for(var i=0; i&lt;winners.length; i++) {
document.write(contestantNamesArray[winners[i]], '&lt;br&gt;' );
}
&lt;/script&gt;
×

Success!

Help @Adytyper 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 5.5,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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