I have a render
function:
render: function(playerId) {
this.getTotalPoints(playerId);
// and some other code after this;
}
This render
function may be executed with or without a playerId
.
This is the getTotalPoints
function:
getTotalPoints: function(playerId) {
if (playerId) {
this.allplayers[playerId].totalPoints = this.calculatePoints(this.allplayers[playerId].cards);
} else {
this.allplayers.forEach(function(element, index) {
element.totalPoints = this.calculatePoints(element.cards);
}.bind(this));
}
}
And the third function that actually calculates the points:
calculatePoints: function(cards) {
points = 0;
for (var i = 0; i < cards.length; i++) {
points += cards[i].points;
};
return points;
}
I am repeating myself in getTotalPoints
, where I have a call to this.calculatePoints
- one for a single player and then one for all the players, depending on whether the playerId
is set or not.
Is there any chance I can avoid this and simplify the code?