[JavaScript] JavaScript Fundamentals – Part 2 Coding Challenge #1
![[JavaScript] JavaScript Fundamentals – Part 2 Coding Challenge #1](https://cdn.hashnode.com/res/hashnode/image/unsplash/ZS67i1HLllo/upload/v1652750395094/HwGh4YAIj.jpeg)
Back to the two gymnastics teams, the Dolphins and the Koalas! There is a new gymnastics discipline, which works differently. Each team competes 3 times, and then the average of the 3 scores is calculated (so one average score per team). A team only wins if it has at least double the average score of the other team. Otherwise, no team wins!
Your tasks:
- Create an arrow function 'calcAverage' to calculate the average of 3 scores
- Use the function to calculate the average for both teams
- Create a function 'checkWinner' that takes the average score of each team as parameters ('avgDolhins' and 'avgKoalas'), and then logs the winner to the console, together with the victory points, according to the rule above. Example: "Koalas win (30 vs. 13)"
- Use the 'checkWinner' function to determine the winner for both Data 1 and Data 2
- Ignore draws this time
Test data:
§ Data 1: Dolphins score 44, 23 and 71. Koalas score 65, 54 and 49 § Data 2: Dolphins score 85, 54 and 41. Koalas score 23, 34 and 27
const calcAverage = (score1, score2, score3) => (score1 + score2 + score3) / 3;
function checkWinner(avgDolphins, avgKoalas) {
if (avgDolphins >= avgKoalas * 2) {
console.log(`Dolphins win (${avgDolphins} vs ${avgKoalas})!`);
} else if (avgKoalas >= avgDolphins * 2) {
console.log(`Koalas win (${avgKoalas} vs ${avgDolphins})!`);
} else {
console.log('No team wins!');
}
return;
}
//Test DATA 1
const avgDolphins1 = calcAverage(44, 23, 71);
const avgKoalas1 = calcAverage(65, 54, 49);
checkWinner(avgDolphins1, avgKoalas1);
//Test DATA 2
const avgDolphins2 = calcAverage(85, 54, 41);
const avgKoalas2 = calcAverage(23, 34, 27);
checkWinner(avgDolphins2, avgKoalas2);
//another way
//Test 1
let avgDolphins = calcAverage(44, 23, 71);
let avgKoalas = calcAverage(65, 54, 49);
checkWinner(avgDolphins, avgKoalas);
//Test 2
avgDolphins = calcAverage(85, 54, 41);
avgKoalas = calcAverage(23, 34, 27);
checkWinner(avgDolphins, avgKoalas);