PDA

View Full Version : Array and for loop

libertyg
06-07-2010, 10:54 PM
HI,
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];
}

//Part(i)(c)
//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)