HTML Academy
Objects and references
Objects9/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. Objects and references
  • 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”
Giving out the attempts
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

My game

Our players roll the dice, get game points. Everything seems to be great, but something is lacking in the game loop. What is it?

Now every cat rolls the dice once, and in our game, the player gets three attempts to roll the dice. It’s time to write this rule as code.

To begin with, we need to add a variable that will contain the number of player attempts. Or could we store it in something else?

The game already has a variable that stores the number of dice. The dice count and the number of attempts together form the game rules. Therefore, we will create a single entity with game rules, object gameRules.

Before adding to the game loop, you have to correct the parameters, arguments and code in the body of the runGame function taking into account the new object with game rules.

Hurry up!

Comments

  • script.js
JavaScript
var diceNumber = 2; var firstCat = { name: 'Muffin', points: 0 }; var secondCat = { name: 'Rudolph', points: 0 }; var cats = [firstCat, secondCat]; var runGame = function (quantity, players) { for (var i = 0; i < players.length; i++) { var throwResult = muffin.throwDice(quantity, quantity * 6); players[i].points += throwResult; console.log(players[i].name + ' rolled ' + players[i].points); } return players; }; cats = runGame(diceNumber, 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. At the beginning of the code, in front of the player objects, create an object gameRules with properties diceNumber: 2 and maxAttempts: 3 .
  2. In runGame function declaration, rename the parameter quantity to rules.
  3. In runGame function call, replace argument diceNumber with gameRules.
  4. In the body of the runGame function, replace the reference to parameter quantity with accessing property diceNumber of parameter rules.
  5. Delete the diceNumber variable.

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

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.