JSFiddle - React, Tailwind, and code Playground

by frosas

HTML

<script src="http://searls.github.com/jasmine-all/jasmine-all-min.js"></script>

JavaScript

var Scoreboard = function() {
    var score = [0, 0];
    
    var getOpponent = function(player) {
        return player === this.PLAYER_1 ? this.PLAYER_2 : this.PLAYER_1;
    };
    
    this.getScore = function() {
        return score.map(function(playerScore, player) {
            switch (playerScore) {
                case Scoreboard.LOVE: return 0;
                case Scoreboard.FIFTEEN: return 15;
                case Scoreboard.THIRTY: return 30;
                case Scoreboard.FORTY: return 40;
                default: // Advantage or win
                    var opponentScore = score[getOpponent(player)];
                    return (opponentScore == Scoreboard.FORTY) ? 'advantage' : 'win';
            }
        });
    };
        
    this.score = function(player) {
        score[player]++;
    };
};

Scoreboard.PLAYER_1 = 0;
Scoreboard.PLAYER_2 = 1;
Scoreboard.LOVE = 0;
Scoreboard.FIFTEEN = 1;
Scoreboard.THIRTY = 2;
Scoreboard.FORTY = 3;

describe("Scoreboard", function() {
    var scoreboard;

    beforeEach(function() {
        scoreboard = new Scoreboard();
    });
    
    it("Initial score should be 0-0", function() {
        expect(scoreboard.getScore()).toEqual([0, 0]);
    });
                
    it("Player 1 scoring one game should result in 15-0", function() {
        scoreboard.score(Scoreboard.PLAYER_1);
        expect(scoreboard.getScore()).toEqual([15, 0]);
    });
    
    it("Player 1 scoring two games should result in 30-0", function() {
        scoreboard.score(Scoreboard.PLAYER_1);
        scoreboard.score(Scoreboard.PLAYER_1);
        expect(scoreboard.getScore()).toEqual([30, 0]);
    });
    
    it("Player 1 scoring three games should result in 40-0", function() {
        scoreboard.score(Scoreboard.PLAYER_1);
        scoreboard.score(Scoreboard.PLAYER_1);
        scoreboard.score(Scoreboard.PLAYER_1);
        expect(scoreboard.getScore()).toEqual([40, 0]);
    });
    
    it("Player 1 scoring four games should result in win",...