Interactive online courses HTML Academy
2026-03-09 10:58 Diff
  • 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; console.log('New record holder: ' + currentPlayer.points); } } return winners; }; cats = runGame(gameRules, cats); console.log(cats); var tops = getWinners(cats); console.log(tops);

Thanks! We’ll fix everything at once!

Result

  1. Write an array of one element max to the winners variable in the check for a new record holder after max = currentPlayer.
  2. Delete log of a message about a record holder in the console.
  3. Add the else branch to the condition.
  4. Inside else, write nested if, which checks equality of points of the current player and those of the record holder (use strict comparison ===).
  5. If this condition is met, add the current player to the winners array using the push method.