View Full Version : Array and for loop

06-07-2010, 11:54 PM
I am new to JavaScript and need help with a code

//names of couples stored in array
var contestantNamesArray = ['Tom and Nazia', 'Pat and Dan', 'Sandra and Kofi', 'Ian and Adele', 'Paul and Costas'];

//points awarded by judges and audience stored in arrays
var judgesPointsArray = [2,1,5,4,3];
var audiencePointsArray = [4,5,2,3,1];

//Part (i)(a)
//new array to store the combined points for each couple
var combinedPointsArray = new Array (4);

//Part (i)(b)
//loop to add the judges and the audience points for each couple and to store them in the combined points array

for (var score = 0; score < combinedPointsArray.length; score = score + 1)
combinedPointsArray[score] = judgesPointsArray[score] + audiencePointsArray[score];

//loop to find and write out the maximum number of points scored

maxPointsScoredIndex = 0;
for (var score = 1; score < combinedPointsArray.length; score = score + 1)
if (combinedPointsArray[score] > combinedPointsArray[maxPointsScoredIndex])
maxPointsScoredIndex = score;
document.write ('The maximum number of points was ' + combinedPointsArray[maxPointsScoredIndex] + '<BR>');

//Part (iii)
//Add code that will
// -- write out a heading for the list of couples scoring the maximum
// -- write out the names of all the couples with the maximum number of combined points
// keeping score of how many they are
// -- at the end, write out whether a dance-off is required or not, depending on how many couples scored the maximum

It's the third part that I'm struggling with.

Could someone help me?
Thank you!