- 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;
};
var printWinners = function (players, winners) {
if (players.length === winners.length) {
console.log('All cats rock!');
}
};
cats = runGame(gameRules, cats);
console.log(cats);
var tops = getWinners(cats);
console.log(tops);
printWinners(cats, tops);
Result
Goalscompleted
Improve the printWinners
function:
- After logging
'All cats rock!'
, end the function withreturn
. - After the first condition, create variable
message
, which contains the line'The winner is '
. - After the variable, write the
for
loop, which increases the counteri
from0
to the length of the arraywinners
(not including this value) by one at each iteration. - In the body of the loop, add the names of the winners
message += winners[i].name
. - After the loop, use
+=
to add the' with the number of points: ' + points of the first player in the winners array
string to themessage
. - Log
message
in the console.
Comments