challenge #1

Javascript Fundamentals Part 2 - jonas.io

by trentHarlem

HTML

<h2>
Javascript Fundamentals Part Deux ~
Coding Challenge #1
</h2>

<p>
Back to the two gymnastics teams, the Dolphins and the Koalas! There is a new gymnastics discipline, which works differently.<br>
Each team competes 3 times, and then the average of the 3 scores is calculated (so one average score per team).<br>
A team ONLY wins if it has at least DOUBLE the average score of the other team. Otherwise, no team wins!
</p>

<p>
1. Create an arrow function 'calcAverage' to calculate the average of 3 scores<br>
2. Use the function to calculate the average for both teams.<br>
3. 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)".<br>
4. Use the 'checkWinner' function to determine the winner for both DATA 1 and DATA 2.<br>
5. Ignore draws this time.<br>
</p>
<p>
TEST DATA 1: Dolphins score 44, 23 and 71. Koalas score 65, 54 and 49<br>
TEST DATA 2: Dolphins score 85, 54 and 41. Koalas score 23, 34 and 27<br>
</p>

<p>
HINT: To calculate average of 3 values, add them all together and divide by 3<br>
HINT: To check if number A is at least double number B, check for A >= 2 * B. Apply this to the team's average scores πŸ˜‰<br>
</p>

GOOD LUCK πŸ˜€

CSS

html {
  font: 1.1em system-ui;
}

JavaScript

// test data 1
let dolphinScores = [44, 23, 71];
let koalaScores = [65, 54, 49]; 

const calcAverage = ([a, b, c]) => (a + b + c) / 3
//const calcAverage = (a, b, c) => (a + b + c) / 3;

const avgDolphins = calcAverage(dolphinScores);
const avgKoalas = calcAverage(koalaScores)
console.log(calcAverage(dolphinScores))
console.log(calcAverage(koalaScores))

function checkWinner(d, k) {
  if (d >= 2 * k || k >= 2 * d) {
    (d > k) ? console.log(`Dolphins Win!πŸ† ${d} vs ${k}`): console.log(`Koalas Win!πŸ† ${k} vs ${d}`);
  } else {
    console.log('No one wins. Boo fackin Hoo')
  }
}
checkWinner(avgDolphins, avgKoalas)
checkWinner(111, 222);

// test data 2
dolphinScores = [85, 54, 41]
koalaScores = [23, 34, 27]
scoreDolphins = calcAverage(85, 54, 41);
scoreKoalas = calcAverage(23, 34, 27);