SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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?

  2. #2
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    any ideas??

  3. #3
    Learning...
    Join Date
    Jan 2003
    Posts
    781
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about doing some thing like this and then building on it?
    Code:
    <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 + '\n'
      alert(print);
      
     </script>
    The beauty of life is not dependent on how happy you are,
    but on how happy others can be because of you...

  4. #4
    Learning...
    Join Date
    Jan 2003
    Posts
    781
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorting an array of objects:
    http://www.javascriptkit.com/javatut...raysort2.shtml

    Code:
    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 + '\n';
      }
      alert(print);
    The beauty of life is not dependent on how happy you are,
    but on how happy others can be because of you...

  5. #5
    Learning...
    Join Date
    Jan 2003
    Posts
    781
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    The beauty of life is not dependent on how happy you are,
    but on how happy others can be because of you...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •