Tennis Score
Prompt
Write a function getTennisScore that takes an array of strings ('p1' or 'p2') representing who won each point in a single tennis game, and returns the current score or final result.
Tennis scoring rules:
- Points progress: 0, 15, 30, 40
- A player at 40 who scores the next point wins, unless the opponent also has 40
- When both players reach 40, it's DEUCE
- From DEUCE, winning one point gives ADVANTAGE
- If the player with ADVANTAGE scores again, they win
- If the player with ADVANTAGE loses the next point, it goes back to DEUCE
Playground
Don't try to compute the tennis score name inside the
loop. Just count how many points each player won. After
the loop, use if/else to figure out the result from the
two totals.
A win happens when a player has at least 4 points AND leads by at least 2. This single condition covers winning from 40 and winning from ADVANTAGE.
If both players have 3 or more points and nobody has won yet, you're in the DEUCE/ADVANTAGE zone. Equal totals means DEUCE, otherwise the player with more points has ADVANTAGE.
Solution
Explanation
The key insight that makes this question easier than it looks: you don't need to track the tennis score as you go through the points. You just need to count the totals, and then figure out the result at the end. The final state of a tennis game depends only on how many points each player won, not the order they won them in.
Step 1: Count the points
We loop through the array once and count how many points each player scored. If the input is ['p1', 'p1', 'p2', 'p1'], we end up with p1Points = 3 and p2Points = 1.
Step 2: Check for a win
A player wins when they have at least 4 points and are ahead by at least 2. This single condition covers every winning scenario: winning straight from 40 (like 4-0, 4-1, 4-2) and winning from advantage (like 5-3, 6-4, 7-5). The "ahead by 2" part is what handles the DEUCE/ADVANTAGE back-and-forth.
For example, if the score is 5-4, the player with 5 is ahead by only 1, so that's ADVANTAGE, not a win. But 5-3? That's ahead by 2, so it's a win.
Step 3: Check for DEUCE or ADVANTAGE
If nobody has won and both players have at least 3 points, we're in the endgame. At this stage, there are only three possibilities: the totals are equal (DEUCE), or one player is ahead by exactly 1 (ADVANTAGE for that player). We already handled the "ahead by 2" case in the win check, so anything left here is either tied or off by one.
Step 4: Standard score
If we get this far, neither player has reached the endgame zone. Both players have fewer than 3 points, or only one of them has 3. We use the scoreNames array (['0', '15', '30', '40']) to convert point counts into their tennis names. scoreNames[2] gives us '30', scoreNames[0] gives us '0', so a 2-0 point count becomes "30 - 0".
Why this order matters
We check conditions from most specific to least specific: win first, then DEUCE/ADVANTAGE, then standard score. If we checked for standard score first, a point count of 4-2 would try to access scoreNames[4] which is undefined. The order protects us from that.