Created
January 12, 2021 21:22
-
-
Save juliangroen/9a5de3dd41b7454fa9d741f02c10da08 to your computer and use it in GitHub Desktop.
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
// score data | |
const dolphinsScores = [97, 112, 101]; | |
const koalasScores = [109, 95, 106]; | |
// helper function to average array values | |
const avgScore = (array) => | |
// array.reduce iterated through an array and adds each value together. | |
// then divide the result agaisn't the legnth of the array to get the average. | |
array.reduce((acc, val) => acc + val) / array.length; | |
// helper function to output results | |
const printResults = (winner, loser, winAvg, loseAvg, dqOne, dqTwo) => { | |
//these use ternary operators, basically a one line IF statement. | |
const resultLine = winAvg !== loseAvg ? `The ${winner} win!!` : `It's a tie!!`; | |
const doubleDq = `Both Teams Disqualified.`; | |
const winAvgLine = `The ${winner} average: ${winAvg}${dqOne === false ? `` : ` - Disqualified`}`; | |
const loseAvgLine = `The ${loser} average: ${loseAvg}${dqTwo === false ? `` : ` - Disqualified`}`; | |
const finalMessage = ` | |
${dqOne && dqTwo ? doubleDq : resultLine} | |
${winAvgLine} | |
${loseAvgLine} | |
`; | |
console.log(finalMessage); | |
}; | |
// wrapping main logic into it's own function | |
const scoreCompare = (teamOne, teamTwo, scoreOne, scoreTwo, dqLimit) => { | |
// check if both scores are above dq limit | |
if (scoreOne >= dqLimit && scoreTwo >= dqLimit) { | |
// team one wins | |
if (scoreOne > scoreTwo) { | |
printResults(teamOne, teamTwo, scoreOne, scoreTwo, false, false); | |
// team two wins | |
} else if (scoreOne < scoreTwo) { | |
printResults(teamTwo, teamOne, scoreTwo, scoreOne, false, false); | |
// tie game | |
} else { | |
printResults(teamOne, teamTwo, scoreOne, scoreTwo, false, false); | |
} | |
// else catches if score one or score two is not above the dq limit | |
} else { | |
// team two dq | |
if (scoreOne >= dqLimit && scoreTwo < dqLimit) { | |
printResults(teamOne, teamTwo, scoreOne, scoreTwo, false, true); | |
// team one dq | |
} else if (scoreOne < dqLimit && scoreTwo >= dqLimit) { | |
printResults(teamTwo, teamOne, scoreTwo, scoreOne, false, true); | |
// double dq | |
} else { | |
printResults(teamOne, teamTwo, scoreOne, scoreTwo, true, true); | |
} | |
} | |
}; | |
// average dolphins scores | |
const avgDolphins = avgScore(dolphinsScores); | |
console.log(avgDolphins); | |
// average koalas scores | |
const avgKoalas = avgScore(koalasScores); | |
console.log(avgKoalas); | |
// run the compare function | |
scoreCompare("Dolphins", "Koalas", avgDolphins, avgKoalas, 100); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment