HTML Academy
Count off!
Objects7/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”
Passing object by a link
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Overriding object properties

We made sure that the loop inside the function works and the current element of the player array is logged in the console. What’s next?

After each roll, we must increase the game points by the result of this roll. We get the following entry player.points + = roll result. How does it work?

Overriding properties

Properties of objects can not only be read, but also overridden, just like ordinary variables. Let’s go back to the cat object.

var cat = {
  name: 'Muffin',
  age: 5
};

cat.age++; // Increasing cat’s age by 1
console.log(cat.age) // Logs 6 in the console

cat.name = 'Rocky'; // Replacing name property value outside
console.log(cat.name); // Logs 'Rocky' in the console

Inside the loop, we’ll call the throwDice roll function, write the result to a variable and increase the game points of the current player by this value. To make it easier to track events in the game, we will log the roll result for each player in the console.

By the way,

As you already know, when we call the array[index], we get array element. In our case, this is a player object, from which we can read as from any other object using dot. Therefore, entry in the body of our loop array[index].key is the same as entry object.key when working with the object directly.

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++) { console.log(players[i]); } }; runGame(diceNumber, 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. Inside the loop in the body of the function, remove element console log.
  2. Within the loop, declare variable throwResult, which contains the result of one roll muffin.throwDice(quantity, quantity * 6);.
  3. Increase the number of roll points for the current player players[i].points += throwResult;.
  4. Log in the console the player’s name and the number of their game points players[i].name + ' rolled ' + players[i].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.