Last active
September 16, 2025 20:32
-
-
Save colinlord/7f9d63482466aa240c14d69edee31be1 to your computer and use it in GitHub Desktop.
Cassidy Interview QOTW: 9/15
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // You are given an array of arrays, where each inner array represents | |
| // the runs scored by each team in an inning of a baseball game: | |
| // [[home1, away1], [home2, away2], ...]. Write a function that returns | |
| // an object with the total runs for each team, which innings each team | |
| // led, and who won the game. | |
| function analyzeBaseballGame(innings) { | |
| let homeTotal = 0; | |
| let awayTotal = 0; | |
| const homeLedInnings = []; | |
| const awayLedInnings = []; | |
| innings.reduce((runningScores, currentInningRuns, index) => { | |
| const [homeInningRuns, awayInningRuns] = currentInningRuns; | |
| const currentHomeScore = runningScores.home + homeInningRuns; | |
| const currentAwayScore = runningScores.away + awayInningRuns; | |
| const inningNumber = index + 1; | |
| if (currentHomeScore > currentAwayScore) { | |
| homeLedInnings.push(inningNumber); | |
| } else if (currentAwayScore > currentHomeScore) { | |
| awayLedInnings.push(inningNumber); | |
| } | |
| return { home: currentHomeScore, away: currentAwayScore }; | |
| }, { home: 0, away: 0 }); | |
| homeTotal = innings.reduce((sum, inning) => sum + inning[0], 0); | |
| awayTotal = innings.reduce((sum, inning) => sum + inning[1], 0); | |
| let winner; | |
| if (homeTotal > awayTotal) { | |
| winner = "home"; | |
| } else if (awayTotal > homeTotal) { | |
| winner = "away"; | |
| } else { | |
| winner = "tie"; | |
| } | |
| return { | |
| homeTotal, | |
| awayTotal, | |
| homeLedInnings, | |
| awayLedInnings, | |
| winner, | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment