HTML Academy
Looking for a cat with great results
Objects14/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”
New conditions
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

No one will hide

We learned how to determine winners and now we can start recording them in array winners. Two interesting situations are possible when going through the players array.

The first situation is when we encounter a player who has more points than the current champion. We already know how to handle this situation. All we have left to do is correctly enter data in the winners array.

We determine record holders one by one, therefore, in the array of winners the player should be in proud solitude. We remove the former record holders from the array and record a new potential champion there. The easiest way to do this is to completely overwrite the array:

// Record an array of one element, max, into winners
winners = [max];

The second situation is when we meet a player with the same number of points as the current contender. We have not yet described this situation. How can we understand that we have met the equal to the most powerful player? For example, like this:

If the current player has more points than the champion,
  then we make him the new winner.
OtherwiseIf the current player has as many points as the champion,
    then add them to the winners' array.

That is, we can use the nested condition inside the alternative branch of the check for the championship.

To add multiple winners to the array, it’s best to use the push method. This method adds the elements transferred to it to the end of the specified array. For example:

var purchases = ['milk', 'kefir'];

purchases.push('sausage', 'candy', 'marmalade');
// Will log ["milk","kefir","sausage","candy","marmalade"]
console.log (purchases);

A lot of everything? It’s okay, train hard, solve the Boss’ tasks easy!

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 = []; var max = players[0]; for (var i = 0; i < players.length; i++) { var currentPlayer = players[i]; if (currentPlayer.points > max.points) { max = currentPlayer; console.log('New record holder: ' + currentPlayer.points); } } 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. Write an array of one element max to the winners variable in the check for a new record holder after max = currentPlayer.
  2. Delete log of a message about a record holder in the console.
  3. Add the else branch to the condition.
  4. Inside else, write nested if, which checks equality of points of the current player and those of the record holder (use strict comparison ===).
  5. If this condition is met, add the current player to the winners array using the push method.

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.