I wrote a fairly simple function to figure out which hole a golfer has completed playing. I'm trying to do it the "pretty" ES6 functional way.
In version 1 I have some old-fashioned JS. Version 2 I think it the most readable and elegant, but ESLint did not like the nested ternary operators. Version 3 made ESLint happy, but I think it's actually harder to follow and required more functions.
Is there a better, cleaner way to do this following the latest design patters and modern syntax?
Here is a JS Fiddle: https://jsfiddle.net/pixelwiz/h3r4nv54/2/
const startedPlayingRound = holesPlayed => holesPlayed > 0;
const finishedPlayingRound = holesPlayed => holesPlayed === 18;
const playingFromFirstTee = (startHole, holesPlayed) => (
startHole === 1 && holesPlayed >= 1 && holesPlayed < 18
);
const wrapAroundLogic = (startHole, holesPlayed) => (
playingFromFirstTee(startHole, holesPlayed) ? holesPlayed :
((startHole + holesPlayed) - 1) % 18
);
const finishedOrNotYetStarted = holesPlayed => (
finishedPlayingRound(holesPlayed) ? 'F' : ''
);
const getThruV1 = (startHole, holesPlayed) => {
let thru = '';
if (startedPlayingRound(holesPlayed)) {
if (finishedPlayingRound(holesPlayed)) {
thru = 'F';
} else if (startHole === 1 && holesPlayed >= 1 && holesPlayed < 18) {
thru = holesPlayed;
} else {
thru = ((startHole + holesPlayed) - 1) % 18;
}
}
return thru.toString();
};
const getThruV2 = (startHole, holesPlayed) =>
!startedPlayingRound(holesPlayed) ? '' :
finishedPlayingRound(holesPlayed) ? 'F' :
wrapAroundLogic(startHole, holesPlayed).toString();
const getThruV3 = (startHole, holesPlayed) => (
finishedPlayingRound(holesPlayed) || !startedPlayingRound(holesPlayed) ?
finishedOrNotYetStarted(holesPlayed) :
wrapAroundLogic(startHole, holesPlayed).toString()
);
$('#v1').html('V1 Result: ' + getThruV1(10, 12));
$('#v2').html('V2 Result: ' + getThruV1(10, 12));
$('#v3').html('V3 Result: ' + getThruV1(10, 12));