Skip to content

Commit

Permalink
add in code for part 3
Browse files Browse the repository at this point in the history
function to calc totals for won, lost, NR, points
  • Loading branch information
afewminutesofcode committed Aug 26, 2019
1 parent 6576bd5 commit 4a0453e
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/__tests__/cricketTeamTotalCalc.js
@@ -0,0 +1,47 @@
import { calcCompletedMatches } from "../helpers/calcCompletedMatches";
import { cricketTeamTotalCalc } from "../points-table/cricketTeamTotalCalc";
import { matches } from "../data/matches";
import {
WON,
LOST,
NORESULT,
POINTS
} from "../helpers/constants";

const config = { pts4Win: 2, pts4NR: 1 };

it("cricketTeamTotalCalc", () => {
expect(
cricketTeamTotalCalc({
config,
matches,
team: "SA",
stat: WON
})
).toBe(3);
expect(
cricketTeamTotalCalc({
config,
matches,
team: "SA",
stat: LOST
})
).toBe(5);
expect(
cricketTeamTotalCalc({
config,
matches,
team: "SA",
stat: NORESULT
})
).toBe(1);

expect(
cricketTeamTotalCalc({
config,
matches,
team: "SA",
stat: POINTS
})
).toBe(7);
});
137 changes: 137 additions & 0 deletions src/points-table/cricketTeamTotalCalc.js
@@ -0,0 +1,137 @@
import {
WON,
LOST,
TEAM1,
TEAM2,
NORESULT,
POINTS
} from "../helpers/constants";
import { teamWon } from "./teamWon";
import { teamWonSuperOver } from "./teamWonSuperOver";

import { teamLost } from "./teamLost";
import { teamLostSuperOver } from "./teamLostSuperOver";

import { teamNoResult } from "./teamNoResult";

export const cricketTeamTotalCalc = ({
config,
matches,
team,
stat
}) => {
const { pts4Win, pts4NR } = config;
// need to look at what is getting passed in here
switch (stat) {
case WON:
return (
teamWon({
matches,
teamNum: TEAM1,
team,
stat: "Ru"
}) +
teamWon({
matches,
teamNum: TEAM2,
team,
stat: "Ru"
}) +
teamWonSuperOver({
matches,
teamNum: TEAM1,
team,
stat: "SupOvrR"
}) +
teamWonSuperOver({
matches,
teamNum: TEAM2,
team,
stat: "SupOvrR"
})
);
case LOST:
return (
teamLost({
matches,
teamNum: TEAM1,
team,
stat: "Ru"
}) +
teamLost({
matches,
teamNum: TEAM2,
team,
stat: "Ru"
}) +
teamLostSuperOver({
matches,
teamNum: TEAM1,
team,
stat: "SupOvrR"
}) +
teamLostSuperOver({
matches,
teamNum: TEAM2,
team,
stat: "SupOvrR"
})
);
case NORESULT:
return (
teamNoResult({
matches,
teamNum: TEAM1,
team
}) +
teamNoResult({
matches,
teamNum: TEAM2,
team
})
);
case POINTS:
return (
(teamWon({
matches,
teamNum: TEAM1,
team,
stat: "Ru"
}) +
teamWon({
matches,
teamNum: TEAM2,
team,
stat: "Ru"
}) +
teamWonSuperOver({
matches,
teamNum: TEAM1,
team,
stat: "SupOvrR"
}) +
teamWonSuperOver({
matches,
teamNum: TEAM2,
team,
stat: "SupOvrR"
})) *
pts4Win +
+(
teamNoResult({
matches,
teamNum: TEAM1,
team
}) +
teamNoResult({
matches,
teamNum: TEAM2,
team
})
) *
pts4NR
);
default:
return;
}
};

0 comments on commit 4a0453e

Please sign in to comment.