HTML Academy
Announce the entire list, please
Objects13/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”
No one will hide
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Looking for a cat with great results

Now that we are sure that the loop is working correctly, we will start looking for the winner, that is, the player with the maximum number of points.

First of all, we need to select a player who we will use to compare the results of other cats with. Since we store players in the array, it is logical and convenient to designate the first participant as the one with the greatest results, at first.

We will sort through the elements of the array in the loop and compare the number of points with the first player. If any other player has a better result, we will designate that player a champion and will continue to compare other results with the ones of this player. To track changes in the winners, we will log the maximum result in the console.

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; }; var getWinners = function (players) { var winners = []; for (var i = 0; i < players.length; i++) { var currentPlayer = players[i]; console.log(currentPlayer); } return winners; }; cats = runGame(gameRules, cats); console.log(cats); var tops = getWinners(cats); console.log(tops);

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. Create variable max that contains the first element of the players array in the body of the getWinners function before the loop.
  2. Inside the loop, instead of console log, add a check to make sure that the current player has more points than the record holder.
  3. If the condition is met, make the current player the champion.
  4. Also log in the console 'New record holder: ' + currentPlayer.points.

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.