HTML Academy
Reading from the object
Objects6/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”
Overriding object properties
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Count off!

With the help of objects, we conveniently described the players and we now use this data in the function. We have fewer parameters, but once again the question arises about the increase in the number of game participants. What if there are ten of them? Should we then create more objects? Yes! But there are two disadvantages:

  • The number of parameters will increase with the number of players.
  • The code in the function body will keep growing, because for each player there is a line for increase in points and log of results in the console. The more players, the longer the code in the function will be.

It would be great to write the function code so that it would work for any number of players. Is there such a structure that can store a lot of data and which is convenient to process? Yes! Arrays! And you are already familiar with them!

Here is a short reminder just in case: an array is a list of elements. Each of them has a sequential number. Arrays and reading from them look like this:

// Number array
var array = [10, 20, 30, 40, 50];

// Let’s read elements from the array by index (sequential number)
console.log(array[0]); // Logs 10
console.log(array[3]); // Logs 40

If you completely forgot what arrays are and how they work, refresh your memory with the “Arrays” chapter.

Let’s return to our task and enter an array with player objects. We will transfer it to the function. Then we can go through the array in the loop and do the same thing for each player: roll the dice, record the result and log it.

Comments

  • script.js
JavaScript
var diceNumber = 2; var firstCat = { name: 'Muffin', points: 0 }; var secondCat = { name: 'Rudolph', points: 0 }; var runGame = function (quantity, firstPlayer, secondPlayer) { firstPlayer.points += muffin.throwDice(quantity, quantity * 6); secondPlayer.points += muffin.throwDice(quantity, quantity * 6); console.log(firstPlayer.name + ' rolled ' + firstPlayer.points); console.log(secondPlayer.name + ' rolled ' + secondPlayer.points); }; runGame(diceNumber, firstCat, secondCat);

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. Create an array cats with firstCat, secondCat elements before function runGame.
  2. Delete parameters firstPlayer and secondPlayer in declaration of function runGame and instead of them add parameter players.
  3. Delete arguments firstCat and secondCat in function call and instead of them add argument cats.
  4. In the body of the runGame function, instead of the entire code, write a loop that increases variable i from 0 to players.length (not including this value). Value of i must increase by one after each iteration.
  5. Inside the loop, log in the console the current element of the players array.

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.