- 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);
Result
Goalscompleted
- After the
getWinners
function, declare a functionprintWinners
with parametersplayers
andwinners
. - At the end of the program, call
printWinners
with argumentscats
andtops
. - Inside the
printWinners
function, add a check for equality of lengths of arraysplayers
andwinners
. - If the condition is fulfilled, log
'All cats rock!'
in the console.
Comments