# Thread: Parallel Arrays

1. Registered User
Join Date
Apr 2010
Posts
20

## 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>')

{
document.write(contestantNamesArray[maxPoints] );
}
```
Thanks

2. Code:
```<script type="text/javascript">
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];
var winners = [];
for (var points = 0; points < judgesPointsArray.length; points++)
{
if (combinedPointsArray[points] >= maxPoints)
{
if(combinedPointsArray[points] > 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 + '<BR>')
document.write('The couple(s) scoring the maximum were:' + '<BR>')

for(var i=0; i<winners.length; i++) {
document.write(contestantNamesArray[winners[i]], '<br>' );
}
</script>```

3. Registered User
Join Date
Apr 2010
Posts
20
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?

4. Not if there is a possibility of multiple winners.
You could combine the first 2 for loops:
Code:
`if ( (judgesPointsArray[points] + audiencePointsArray[points]) >= maxPoints)`

5. Registered User
Join Date
Apr 2010
Posts
20
So where could i add this loop for it to work?

6. Code:
```<script type="text/javascript">
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 < judgesPointsArray.length; points++)
{
var temp = judgesPointsArray[points] + audiencePointsArray[points];
if (temp >= maxPoints)
{
if(temp > 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 + '<BR>')
document.write('The couple(s) scoring the maximum were:<BR>')

for(var i=0; i<winners.length; i++) {
document.write(contestantNamesArray[winners[i]], '<br>' );
}
</script>```