0

I have the following code and its output is 'undefined', but I have checked the logic and seems correct.

const calcAverage = (score1, score2, score3) => ((score1 + score2 + score3) / 3);

const dolphins_avg = calcAverage(44, 23, 71);
const koalas_avg = calcAverage(65, 54, 49);

console.log(dolphins_avg);
console.log(koalas_avg);

function checkWinner(Koalasavg1, Dolphinsavg2) {
  let winner;
  if (Koalasavg1 > Dolphinsavg2 && (Koalasavg1 >= Dolphinsavg2 * 2)) {
    let winner = `Koalas wins (${Koalasavg1} vs. ${Dolphinsavg2})`;
  }
  else if (Dolphinsavg2 > Koalasavg1 && (Dolphinsavg2 >= Koalasavg1)) {
    let winner = `Dolphins win (${Dolphinsavg2} vs. ${Koalasavg1})`;
  }
  else {
    let winner = `No one wins!`;
  }

  return winner;
}

console.log(checkWinner(koalas_avg, dolphins_avg));
  • Variables declared with `let` are only visible within the block in which it's declared. Remove the `let`s from the inner ones so you're reassigning the outer variable instead of declaring a new inner variable. Or, better yet, just `return` immediately instead of using variables. – CertainPerformance Aug 18 '21 at 21:50

0 Answers0