HTML Academy
My attempt number one
Objects4/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”
Reading from the object
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Hello, object!

Each player rolls dice and gets game points. Everything is working, but note the number of parameters in the runGame function. What happens if there are three, four, five, ten players? And if we get more characteristics for each player? The answer is obvious: a function with forty-two parameters. But we won’t do it this way.

Each cat has related information: a name and points. Is there a more convenient way to store values ​​related to one entity? Yes! We’ll use objects.

An object is a data type that stores information as key-value pairs. An array gives each element a numbered index, while an object lets us access values by their keys. In this example, we use those keys rather than rely on the order of properties. Let’s look at an example.

First, we must create an object. To do this, use the curly brackets that you are already familiar with { }. Store the object in a variable, choosing its name just as you would for any other variable.

var cat = {};
// This creates an object and stores it in the variable cat

Next, we’ll add properties. The key-value pairs that the object contains are called properties or attributes. They look like this:

var cat = {
  name: 'Muffin',
  // We added a new property where the name is key, and 'Muffin' is a value
  age: 5
  // New property. The key is age, the value is 5
};

Properties are referred to by the name of the key. That is, we can say that the cat object has a property for storing the name, where the key is name, and the value is 'Muffin', or that the cat object has the name property with value 'Muffin'.

Several syntax rules:

  • The key is separated from the value by a colon.
  • Key-value pairs are separated from each other by commas.
  • Values ​​can be any type of data (number, string, array, and so on).

Let’s create players’ objects instead of a set of variables. Each player will have two properties: one for the name and one for game points.

By the way,

Writing an object with all properties in curly brackets is called an object literal. If you need to create an object with a literal, you just need to use curly brackets. And if the object has some properties, write them inside these brackets.

Comments

  • script.js
JavaScript
var diceNumber = 2; var firstCatName = 'Muffin'; var firstCatPoints = 0; var secondCatName = 'Rudolph'; var secondCatPoints = 0; var runGame = function (quantity, firstPlayerName, firstPlayerPoints, secondPlayerName, secondPlayerPoints) { firstPlayerPoints += muffin.throwDice(quantity, quantity * 6); secondPlayerPoints += muffin.throwDice(quantity, quantity * 6); console.log(firstPlayerName + ' rolled ' + firstPlayerPoints); console.log(secondPlayerName + ' rolled ' + secondPlayerPoints); }; runGame(diceNumber, firstCatName, firstCatPoints, secondCatName, secondCatPoints);

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. Before the runGame function, add the first player object firstCat.
  2. Add the property with the player’s name: key name, value 'Muffin' to object firstCat.
  3. After the firstCat object, add the secondCat object with the name 'Rudolph'.
  4. Add to each object a property for storing the points: key points, value 0.

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.