HTML Academy
Previous task
Objects1/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”
Players, let’s start!
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Shall we play?

You have some free time to spare between running endless errands for the Boss. Why not write something for yourself, like for rolling dice?

The principle is that players take turns rolling two dice. The result of the roll is the participant’s points. The winner is the one who scored the most points. To create excitement, each player will have three attempts.

We will write the program step-by-step and start with creating the runGame function where in the future the game will be launched from (rolling dice and accumulating points).

To roll dice, use the function muffin.throwDice(min, max), which generates random numbers in a given interval, including the minimum and maximum values. This function exists only in the course. In JavaScript, there is no such function, but there are others that perform similar actions. We’ll talk about them later in other courses. In the meantime, we will use muffin.throwDice(min, max). What are the minimum and maximum values?

If you roll one dice, you can get a number from 1 to 6, because a dice has six faces. And if you roll two dice, you can get a number from 2 to 12.

So we transfer arguments 2 and 12 to the function? No, we’ll do it in a better way! Let’s attach the arguments to the number of dice: if we have one dice, then the values ​​of the arguments will be the same, if there are three dice, then we will have different values.

The worst result in the game is obtained when all dice get a one and the best: if all dice get a six. Therefore, the minimum value is equal to the number of dice, and the maximum value is 6 * number of dice.

All we have left to do is declare a variable that will store the number of dice and use it to calculate the arguments of the runGame function.

Let’s roll the dice and make sure that the function works.

Let the game begin!

Comments

  • script.js
JavaScript

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. Declare variable diceNumber with value 2.
  2. Create a function runGame with parameter quantity.
  3. In the body of the function, log a result of one roll muffin.throwDice(quantity, quantity * 6) in the console.
  4. Call the runGame function with argument diceNumber.

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.