HTML Academy
Giving out the attempts
Objects11/30
Back to the list of tasks
  • 1. Shall we play?
  • 2. Players, let’s start!
  • 3. My attempt number one
  • 4. Hello, object!
  • 5. Reading from the object
  • 6. Count off!
  • 7. Overriding object properties
  • 8. Passing object by a link
  • 9. My game
  • 10. Giving out the attempts
  • 11. Who is the winner?
  • 12. Announce the entire list, please
  • 13. Looking for a cat with great results
  • 14. No one will hide
  • 15. New conditions
  • 16. Hard to come across
  • 17. Let’s bring it all to light
  • 18. Roll the dice, gentlemen cats!
  • 19. Let’s make adjustments
  • 20. Summary of “Objects”. Part 1
  • 21. Tenth program: “Golden ball”
  • 22. Build it yourself!
  • 23. My first method
  • 24. Implementing methods
  • 25. Object as a dictionary
  • 26. Bracket notation
  • 27. Let’s not forget about the context
  • 28. Store check
  • 29. Summary of “Objects”. Part 2
  • 30. Eleventh program: “The house that Muffin built”
Announce the entire list, please
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Who is the winner?

We did the most difficult thing: broke the game into components and wrote a game loop!

So far we can determine the winner of the game by looking at the list of game points in the console. We will make it so that the program determines the champion. We will write the code in several stages, checking the program at every step.

Create the getWinners function, which will receive an array of players, process it and determine the winners.

The task of finding the champion is similar to finding the maximum element in the array: we need to walk through the array of players and find the one who scored the most points. There is one difficulty here: there can be more than one winner, because in the course of the game, several cats can score the same number of points. What should we do in this case?

Let’s optimize the algorithm for finding the maximum element in the array so that all players with the most points are counted. To do this, we will create a winner array. Most often this array will consist of one element, but if there are several champions in the game, we will not offend anyone and record all the winners.

To start, we’ll enter an empty array, return it from getWinners and check that everything works.

Comments

  • script.js
JavaScript
var gameRules = { diceNumber: 2, maxAttempts: 3 }; var firstCat = { name: 'Muffin', points: 0 }; var secondCat = { name: 'Rudolph', points: 0 }; var cats = [firstCat, secondCat]; var runGame = function (rules, players) { for (var currentAttempt = 1; currentAttempt <= rules.maxAttempts; currentAttempt++) { for (var i = 0; i < players.length; i++) { var throwResult = muffin.throwDice(rules.diceNumber, rules.diceNumber * 6); players[i].points += throwResult; console.log(players[i].name + ' rolled ' + players[i].points); } } return players; }; cats = runGame(gameRules, cats); console.log(cats);

What didn’t you like in this task?

Thanks! We’ll fix everything at once!

Console

The code has changed, click “Run” or turn autorun on.

Result

Goalscompleted
  1. After the runGame function, declare the getWinners function parameter players.
  2. In the body of the function, declare empty array winners and return it from the function.
  3. In the code of the program, after logging in the console of the cats array, create variable tops and write in it the result of the function getWinners(cats).
  4. Log variable tops in the console.

Cookies ∙ Privacy ∙ License Agreement ∙ About ∙ Contacts ∙ © HTML Academy OÜ, 2019−2025

VISAMastercard

Log in

or

Forgot your password?

Sign up

Sign up

or
Log in

Restore access

Have you forgotten your password or lost access to your profile? Enter your email connected to your profile and we will send you a link to restore access.

Forgot to connect your email to the profile? Email us and we’ll help.