- 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 = [];
for (var i = 0; i < players.length; i++) {
var currentPlayer = players[i];
console.log(currentPlayer);
}
return winners;
};
cats = runGame(gameRules, cats);
console.log(cats);
var tops = getWinners(cats);
console.log(tops);
Result
Goalscompleted
- Create variable
max
that contains the first element of theplayers
array in the body of thegetWinners
function before the loop. - Inside the loop, instead of console log, add a check to make sure that the current player has more points than the record holder.
- If the condition is met, make the current player the champion.
- Also log in the console
'New record holder: ' + currentPlayer.points
.
Comments