HTML Academy
No one will hide
Objects15/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”
Hard to come across
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

New conditions

We used nested conditions to add a check to test to several winners with the same results. Such conditions work, but they make the code difficult to read.

Let’s look at a simple everyday example. You went to a store to get some milk. If there’s no milk in the store, but there is some kefir then you’ll buy it. If there is no kefir, you’ll have to get some yogurt. And if they’re out of yogurt, you’ll have to buy soy milk, although you do not really like it. The code with nested conditions will look like this:

var isMilk = false;
var isKefir = false;
var isYogurt = true;

if (isMilk) {
  console.log('Excellent! I’ll take some milk!');
} else {
  if (isKefir) {
    console.log ('Then I’ll take some kefir!');
  } else {
    if (isYogurt) {
      console.log ('Oh well! I’ll buy some yogurt!');
    } else {
      console.log ('Well, I’ll have to take soy milk!');
    }
  }
};

// Logs 'Oh well! I’ll buy some yogurt!' in the console

The code looks cumbersome, don’t you think? There is a more convenient way to write it: a chain of conditional structures. It works when it is necessary to check more than two conditions and complete the check if any of the conditions are triggered.

The syntax is familiar to you, only in the second and subsequent branches, if can be separated by space immediately after else, and then a new condition can be added. Then, if the first check is not performed, the program will check the condition in the next branch. If the condition is wrong there, the code will be checked further. At the end of the structure, you can add a branch with the standard else if none of the previous conditions are met.

With the chain of conditions, the code for a trip to the store will look like this:

var isMilk = false;
var isKefir = false;
var isYogurt = true;

if (isMilk) {
  console.log('Excellent! I’ll take some milk!');
} else if (isKefir) {
  console.log ('Then I’ll take some kefir!');
} else if (isYogurt) {
  console.log ('Oh well! I’ll buy some yogurt!');
} else {
  console.log ('Well, I’ll have to take some soy milk!');
}

// Logs 'Oh well! I’ll buy some yogurt!' in the console

This way the code looks neater and it is easy to read.

Use the chain of conditions in our code.

Comments

  • script.js
JavaScript
var gameRules = { diceNumber: 2, maxAttempts: 3 }; var firstCat = { name: 'Muffin', points: 0 }; var secondCat = { name: 'Rudolph', points: 0 }; var cats = [firstCat, secondCat]; var runGame = function (rules, players) { for (var currentAttempt = 1; currentAttempt <= rules.maxAttempts; currentAttempt++) { for (var i = 0; i < players.length; i++) { var throwResult = muffin.throwDice(rules.diceNumber, rules.diceNumber * 6); players[i].points += throwResult; console.log(players[i].name + ' rolled ' + players[i].points); } } return players; }; var getWinners = function (players) { var winners = []; var max = players[0]; for (var i = 0; i < players.length; i++) { var currentPlayer = players[i]; if (currentPlayer.points > max.points) { max = currentPlayer; winners = [max]; } else { if (currentPlayer.points === max.points) { winners.push(currentPlayer); } } } return winners; }; cats = runGame(gameRules, cats); console.log(cats); var tops = getWinners(cats); console.log(tops);

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. Replace the nested condition in the getWinners function with else if.

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.