JavaScript Arrays

Hi Everyone,

I am stuck on a query…I will show what I have so far.

<SCRIPT LANGUAGE = “JavaScript”>

//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];

//Add code to declare a new array to store the combined points for each couple
var combinedPointsArray = [‘contestantNamesArray’ + ‘judgesPointsArray’] I dont think the part I have written here is correct! I need this section or array to combine and store the points for each couple.

I also need to add code which loops through the couples and adds the points from the judges to the points from the audience, storing the result at the corresponding location in combinedPointsArray.

And lasly add code to find and write out the maximum value that is found in combinedPointsArray.

//Add code to calculate the combined points for each couple and store it in the combined points array

//Add code to find and output the maximum value in the combined points array

//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 count 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

</SCRIPT>

I would appreciate some help if possible and explain to me how it was acheive and where I had gone wrong?

I think the sort should first sort on the combined points and then on contestant names. If 2 contestants have the same points then they should show in alphabetical order. I have tried this and failed. Some one else will need to help you here.

Sorting an array of objects:


function data(contestants, pointsJudges, pointsAudience){
   this.Contestants = contestants;
   this.PointsJudges = pointsJudges;
   this.PointsAudience = pointsAudience;
   this.CombinedPoints = pointsJudges+pointsAudience;
  }
  var item = new Array();
  item[0] = new data('Tom and Nazia', 23, 30);
  item[1] = new data('Pat and Dan', 30, 25);
  item[2] = new data('Sandra and Kofi', 31, 29);
  item[3] = new data('Ian and Adele', 15, 25);
  item[4] = new data('Paul and Costas', 27, 29);
  //sort
  item.sort(function(a, b){
   return a.CombinedPoints-b.CombinedPoints
  });
  //print
  var print = '';
  for(var i=0; i<item.length; i++){
   print += item[i].Contestants + ' | ' + item[i].PointsJudges + ' | ' + item[i].PointsAudience + ' | ' + item[i].CombinedPoints + '\
';
  }
  alert(print);

How about doing some thing like this and then building on it?


<script type="text/javascript"> 
  function data(contestants, pointsJudges, pointsAudience){
   this.Contestants = contestants;
   this.PointsJudges = pointsJudges;
   this.PointsAudience = pointsAudience;
  }
  var item = new Array();
  item[0] = new data('Tom and Nazia', 1,2);
  item[1] = new data('Pat and Dan', 11, 22);
  item[2] = new data('Sandra and Kofi', 3, 33);
  item[3] = new data('Ian and Adele', 4, 44);
  item[4] = new data('Paul and Costas', 5, 55);
  //print
  var print = '';
  for(var i=0; i<item.length; i++)
   print += item[i].Contestants + ' | ' + item[i].PointsJudges + ' | ' + item[i].PointsAudience + '\
'
  alert(print);
  
 </script>

any ideas??