HTML Academy
Roll the dice, gentlemen cats!
Objects19/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”
Summary of “Objects”. Part 1
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Let’s make adjustments

It seems the program is working as it should. Almost.

If one player wins, then we get an excellent message like The winner is Player with the following number of points: number.

If there are several winners (and there are losers), then the message looks worse: The winner is PlayerPlayerPlayer with the following number of points: number. Something seems to have gone wrong.

The player names in this message must be separated by commas and spaces. And, of course, if there are several winners, it should be The winners are instead of The winner is. Programs are programs, but we still have to use grammatically correct English.

To make the message for the winners look more acceptable, you’ll have to add a couple of small checks to the printWinners function.

Comments

  • script.js
JavaScript
var gameRules = { diceNumber: 1, maxAttempts: 1 }; var firstCat = { name: 'Muffin', points: 0 }; var secondCat = { name: 'Rudolph', points: 0 }; var thirdCat = { name: 'Rocky', points: 0 }; var cats = [firstCat, secondCat, thirdCat]; 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; winners = [max]; } else if (currentPlayer.points === max.points) { winners.push(currentPlayer); } } return winners; }; var printWinners = function (players, winners) { if (players.length === winners.length) { console.log('All cats rock!'); return; } var message = 'The winner is '; for (var i = 0; i < winners.length; i++) { message += winners[i].name; } message += ' with the number of points: ' + winners[0].points; console.log(message); }; cats = runGame(gameRules, cats); console.log(cats); var tops = getWinners(cats); console.log(tops); printWinners(cats, 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

In the body of the printWinners function:

  1. After declaring the variable message, add a check to make sure that the number of winners is greater than 1.
  2. If the condition is true, override the value of the variable message to 'The winners are '.
  3. At the beginning of the loop body, add a check to make sure that i is greater than or equal to 1.
  4. If the condition is met, add ', ' string to message (that way a comma will be added between all the names if there are more than one).
  5. Remove console log for arrays cats and tops at the end of the program.

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.