Skip to content

Instantly share code, notes, and snippets.

@MrTortoise
Created August 2, 2023 11:10
Show Gist options
  • Save MrTortoise/028f8d04c2cfe6be5d2e321567da46e8 to your computer and use it in GitHub Desktop.
Save MrTortoise/028f8d04c2cfe6be5d2e321567da46e8 to your computer and use it in GitHub Desktop.
tennis started
// tennis scoring a game
// love, 15, 30, 40 -> game
// unless both 40 its deuce
// player1: adv -> game (if p1 scores)
// adv -> deuce (if p2 scores)
// enter a score
type Command = 'start game'| 'player1' | 'player2'
type State =
| 'not started'
| 'love-love'
| '15-15'
| '30-15'
| '30-30'
| '40-15'
const score = (currentScore: State, command: Command): State => {
if(command == 'start game' && currentScore == 'not started') return 'love-love'
if(command === 'player2' && currentScore == '30-15') return '30-30'
if (command == 'player1' && currentScore == '15-15') {
return '30-15'
}
return '40-15'
}
describe('tennis scoring will', () => {
test('game starts at love-love', () => {
expect(score('not started', 'start game')).toBe('love-love')
})
test('15-15, play1scores score should be 30-15', () => {
expect(score('15-15', 'player1')).toBe('30-15')
})
test('30-15, play1scores score should be 40-15', () => {
expect(score('30-15', 'player1')).toBe('40-15')
})
test('30-15, play2scores score should be 30-30', () => {
expect(score('30-15', 'player2')).toBe('30-30')
})
test('15-15, play1scores score should be 30-15', () => {
expect(score('15-15', 'player1')).toBe('30-15')
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment