Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2010
    Location
    England
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Finding two or more matching largest values in an array.

    Hi I am currently writing a program to analyse (to a degree), the points scored in a dance contest. The program mainly works so far. However I am struggling to conclude the program end. Using an if statement, I would like to compute if a dance-off is required, looping through the combinedPointsArray and determining if 2 or more numbers hold the higest score and are equal.

    The aim is to provide:
    1. Maximum number of combined points
    2. The couples and their combined points
    3. The couples names with the highest points
    4. If two or more couples have equal highest combined points - output whether a dance-off is required.


    This is my program so far:


    Code:
    <HTML>
    <HEAD>
    <LINK REL="shortcut icon" TYPE="image/x-icon" HREF="favicon.ico">
    <TITLE>
    Latin Dancing Contest
    </TITLE>
    
    <SCRIPT LANGUAGE = "JavaScript">
    
    var contestantNamesArray = ['Tom and Nazia', 'Pat and Dan', 'Sandra and Kofi', 'Ian and Adele', 'Paul and Costas'];
    
    var judgesPointsArray = [2,1,5,4,3];
    var audiencePointsArray = [4,5,2,3,1];
    var combinedPointsArray = new Array (judgesPointsArray.length)
    
    for (x=0;x<combinedPointsArray.length;x++) 
    	{
        combinedPointsArray[x] = parseInt( judgesPointsArray[x] +  audiencePointsArray[x] );
    	}
    
    
    var biggest = 0;
    var values = new Array();
    
    for (x=0;x<combinedPointsArray.length;x++) 
    
        {
    	if (combinedPointsArray[x] >= biggest ) 
    	  {  
          values[biggest]++;
          biggest = parseInt(combinedPointsArray[x]);
    	  }
    	
          }
    	  
    		if ( values == biggest ) 
           {
           document.write('A Dance-off is required <BR><BR> ');
           }
    	   
    	   document.write('The biggest score was : ' + (biggest));
    	
    
    	
    </script> 
    
    <BR><BR>
    <table border="1"> 
    <tr><th>Names</th><th>Combined Points</th></tr> 
    
    <script language="JavaScript"> 
    <!-- 
    // Use a for loop to step through the two arrays and print 
    // the values in the table. 
      for (var i=0; i<contestantNamesArray.length; i++) 
    	{ 
    	document.write("<tr><td>" + contestantNamesArray[i] + "</td>"); 
    	document.write("<td>" + combinedPointsArray[i] + "</td></tr>"); 
    	} 
    //--> 
    
    
    </script> 
    
    </HEAD>
    <BODY>
    </BODY>
    </HTML>
    Any help would be really appreciated. I have spent so long on this; consequently I am pulling my hair out.

    Regards

    hyp3r

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Have you tried the Search feature of this forum? We have done this homework already this semester.

    Problem linking with Arrays, For and If loops


    It is your responsibility to die() if necessary….. - PHP Manual

  • Users who have thanked Philip M for this post:

    Hyp3r (07-10-2010)

  • #3
    New to the CF scene
    Join Date
    Jul 2010
    Location
    England
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thank you, I did try the search function but to no avail.

  • #4
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,495
    Thanks
    3
    Thanked 500 Times in 487 Posts
    Code:
    <HTML>
    <HEAD>
    <LINK REL="shortcut icon" TYPE="image/x-icon" HREF="favicon.ico">
    <TITLE>
    Latin Dancing Contest
    </TITLE>
    
    <SCRIPT LANGUAGE = "JavaScript">
    
    var contestantNamesArray = ['Tom and Nazia', 'Pat and Dan', 'Sandra and Kofi', 'Ian and Adele', 'Paul and Costas'];
    
    var judgesPointsArray =   [2,1,5,4,3];
    var audiencePointsArray = [4,5,2,3,1];
    var combinedPointsArray = [];
    
    for (var x=0;x<contestantNamesArray.length;x++){
     combinedPointsArray[x] = [contestantNamesArray[x],judgesPointsArray[x] +  audiencePointsArray[x]];
    }
    
    
    var biggest = 0;
    var values = [];
    
    for (var x=0;x<combinedPointsArray.length;x++){
     if (combinedPointsArray[x][1]>= biggest ){
      biggest = combinedPointsArray[x][1];
     }
    }
    
    for (var x=0;x<combinedPointsArray.length;x++){
     if (combinedPointsArray[x][1]== biggest ){
       values.push(combinedPointsArray[x][0]);
     }
    }
    
    if (values.length>1){
     document.write('A Dance-off is required <BR><BR> ');
     document.write(values.join('<BR>')+'<BR><BR>');
    }
    
     document.write('The biggest score was : ' + biggest);
    
    
    
    </script>
    
    <BR><BR>
    <table border="1">
    <tr><th>Names</th><th>Combined Points</th></tr>
    
    <script language="JavaScript">
    <!--
    // Use a for loop to step through the two arrays and print
    // the values in the table.
      for (var i=0; i<contestantNamesArray.length; i++)
    	{
    	document.write("<tr><td>" + combinedPointsArray[i][0] + "</td>");
    	document.write("<td>" + combinedPointsArray[i][1] + "</td></tr>");
    	}
    //-->
    
    
    </script>
    
    </HEAD>
    <BODY>
    </BODY>
    </HTML>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • Users who have thanked vwphillips for this post:

    Hyp3r (07-10-2010)

  • #5
    New to the CF scene
    Join Date
    Jul 2010
    Location
    England
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Although the 'homework' had been done before. I wanted to do it from scratch. You certainly don't learn anything by copying. Also I took the initiative to post the contestantsNamesArray + combinedPointsArray in a table. Much neater imo.


    Thanks for your help guys.

    p.s I don't mind admitting I'm a newbie at programming.

    Year 1 of my BSc computing and systems practice degree


  •  

    Tags for this Thread

    Posting Permissions

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