www.webdeveloper.com
Results 1 to 6 of 6

Thread: Parallel Arrays

  1. #1
    Join Date
    Apr 2010
    Posts
    20

    Arrow Parallel Arrays

    Hi

    I have the following code, and want to express the max points result in parallel with the winners names (i.e the two couples who got 7), What do i need to do to amend this code at the bottom?

    Code:
    var contestantNamesArray = ['bob and pete', 'tim and jon', 'mick and alun', 'van and brad', 'chez and jaz'];
    
    var judgesPointsArray = [2,1,5,4,3];
    var audiencePointsArray = [4,5,2,3,1];
    
    var combinedPointsArray = new Array (5);
    
    for (var points = 0; points < judgesPointsArray.length; points++) 
    {
        combinedPointsArray[points] = judgesPointsArray[points] + audiencePointsArray[points];
    }
    
    
    var maxPoints = combinedPointsArray[0]
    
    for (var points = 0; points < judgesPointsArray.length; points++) 
    {
    	if (combinedPointsArray[points] > maxPoints)
    	{
    		maxPoints = combinedPointsArray[points];
    	}
    }
    document.write('The maximum number of points was ' + maxPoints + '<BR>')
    
    
    
    document.write('The couple(s) scoring the maximum were:' + '<BR>') 
    
    
    {
    document.write(contestantNamesArray[maxPoints] );
    }
    
    Thanks

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    <script type="text/javascript">
    var contestantNamesArray = ['bob and pete', 'tim and jon', 'mick and alun', 'van and brad', 'chez and jaz'];
    var judgesPointsArray = [2,1,5,4,3];
    var audiencePointsArray = [4,5,2,3,1];
    var combinedPointsArray = new Array (5);
    
    for (var points = 0; points < judgesPointsArray.length; points++) 
    {
        combinedPointsArray[points] = judgesPointsArray[points] + audiencePointsArray[points];
    }
    
    var maxPoints = combinedPointsArray[0];
    var winners = [];
    for (var points = 0; points < judgesPointsArray.length; points++) 
    {
    	if (combinedPointsArray[points] >= maxPoints)
    	{
            if(combinedPointsArray[points] > maxPoints) {winners.length = 0;} // empty array, new max points
            winners.push(points); // save the winner
    		maxPoints = combinedPointsArray[points];
    	}
    }
    document.write('The maximum number of points was ' + maxPoints + '<BR>')
    document.write('The couple(s) scoring the maximum were:' + '<BR>') 
    
    for(var i=0; i<winners.length; i++) {
    document.write(contestantNamesArray[winners[i]], '<br>' );
    }
    </script>
    At least 98% of internet users' DNA is identical to that of chimpanzees

  3. #3
    Join Date
    Apr 2010
    Posts
    20
    Thanks, it's not quite what i'm after doing.

    I dont want to assign another variable, i.e "winners", so is there a way of doing it?

  4. #4
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Not if there is a possibility of multiple winners.
    You could combine the first 2 for loops:
    Code:
    if ( (judgesPointsArray[points] + audiencePointsArray[points]) >= maxPoints)
    At least 98% of internet users' DNA is identical to that of chimpanzees

  5. #5
    Join Date
    Apr 2010
    Posts
    20
    So where could i add this loop for it to work?

  6. #6
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    <script type="text/javascript">
    var contestantNamesArray = ['bob and pete', 'tim and jon', 'mick and alun', 'van and brad', 'chez and jaz'];
    var judgesPointsArray = [2,1,5,4,3];
    var audiencePointsArray = [4,5,2,3,1];
    
    var maxPoints = 0;
    var winners = [];
    for (var points = 0; points < judgesPointsArray.length; points++) 
    {
    	var temp = judgesPointsArray[points] + audiencePointsArray[points];
        if (temp >= maxPoints)
    	{
            if(temp > maxPoints) {winners.length = 0;} // empty array, new max points
            winners.push(points); // save the winner
    		maxPoints = temp;
    	}
    }
    document.write('The maximum number of points was ' + maxPoints + '<BR>')
    document.write('The couple(s) scoring the maximum were:<BR>') 
    
    for(var i=0; i<winners.length; i++) {
    document.write(contestantNamesArray[winners[i]], '<br>' );
    }
    </script>
    At least 98% of internet users' DNA is identical to that of chimpanzees

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles