Skip to content

Instantly share code, notes, and snippets.

@colinlord
Last active September 16, 2025 20:32
Show Gist options
  • Select an option

  • Save colinlord/7f9d63482466aa240c14d69edee31be1 to your computer and use it in GitHub Desktop.

Select an option

Save colinlord/7f9d63482466aa240c14d69edee31be1 to your computer and use it in GitHub Desktop.
Cassidy Interview QOTW: 9/15
// 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