Skip to content

Instantly share code, notes, and snippets.

@czeildi
Created June 27, 2017 19:51
Show Gist options
  • Save czeildi/772c35aee8aa52cbac18995d9e8dfb84 to your computer and use it in GitHub Desktop.
Save czeildi/772c35aee8aa52cbac18995d9e8dfb84 to your computer and use it in GitHub Desktop.
tennis refactoring kata solution
var TennisGame1 = function(player1Name, player2Name) {
this.scores = [
0,
0
];
this.players = [
player1Name,
player2Name
];
this.hyphen = '-';
this.whiteSpace = ' ';
};
TennisGame1.prototype.isDraw = function() {
return this.scores[0] === this.scores[1];
};
TennisGame1.prototype.isDeuce = function() {
return this.scores[0] >= 3 && this.isDraw();
};
TennisGame1.prototype.isEndGame = function() {
return Math.max(this.scores[0], this.scores[1]) >= 4;
};
TennisGame1.prototype.pointCode = function(playerIndex) {
if (playerIndex === 1 && this.isDraw()) {
return 'All';
}
return [
'Love',
'Fifteen',
'Thirty',
'Forty'
][this.scores[playerIndex]];
};
TennisGame1.prototype.getPlayerInAdvance = function() {
if (this.scores[0] > this.scores[1]) {
return this.players[0];
}
return this.players[1];
};
TennisGame1.prototype.getWinSituation = function() {
if (this.isAdvantage()) {
return 'Advantage';
}
return 'Win for';
};
TennisGame1.prototype.isAdvantage = function() {
return Math.abs(this.scores[0] - this.scores[1]) === 1;
};
TennisGame1.prototype.wonPoint = function(playerName) {
this.scores[this.players.indexOf(playerName)] += 1;
};
TennisGame1.prototype.getScore = function() {
var score;
if (this.isDeuce()) {
score = 'Deuce';
} else if (this.isEndGame()) {
score = this.getWinSituation() + this.whiteSpace + this.getPlayerInAdvance();
} else {
score = this.pointCode(0) + this.hyphen + this.pointCode(1);
}
return score;
};
if (typeof window === "undefined") {
module.exports = TennisGame1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment