-2

I don't know why I keep getting the console.log from if statement even both averages are above 100. Where did I put my mistake?

const dolphinsScores = 97 + 112 + 101;
const dolphinsAverage = dolphinsScores / 3;

const koalasScores = 109 + 95 + 123;
const koalasAverage = koalasScores / 3;

console.log(dolphinsAverage);
console.log(koalasAverage);

if (dolphinsAverage || koalasAverage < 100) {
    console.log('One of the team is already lost')
} else if (dolphinsAverage > koalasAverage) {
    console.log('Team Dolphins are the winner of the competition! ');
} else if (koalasAverage > dolphinsAverage) {
    console.log('Team Koalas are the winner of the competition! ');
}
Chimpurok
  • 5
  • 2
  • 1
    `dolphinsAverage || koalasAverage < 100` does not what you expect. –  Jun 02 '21 at 10:08
  • `dolphinsAverage || koalasAverage < 100` - this condition means if `dolphinsAverage` is a truthy value OR if `koalasAverage < 100` - change it to: `dolphinsAverage < 100 || koalasAverage < 100` – Yousaf Jun 02 '21 at 10:09
  • You if logic is wrong. Should be: ``if (dolphinsAverage < 100 || koalasAverage < 100)`` – programandoconro Jun 02 '21 at 10:09
  • Check out [this table about operator precedence](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#table) to find out why you end up within your if statement – ferdynator Jun 02 '21 at 10:14

4 Answers4

1

Your if condition was checking if there is any value in dolphinsAverage which will always be true if there is any value in dolphinsAverage. So try this if you want to know if dolphinsAverage's value is less than 100.

if (dolphinsAverage < 100 || koalasAverage < 100) {
    console.log('One of the team is already lost')
} else if (dolphinsAverage > koalasAverage) {
    console.log('Team Dolphins are the winner of the competition! ');
} else if (koalasAverage > dolphinsAverage) {
    console.log('Team Koalas are the winner of the competition! ');
}
M.Hassan Nasir
  • 851
  • 2
  • 13
  • This is one of the most common beginner's mistakes in programming and guaranteed to be a duplicate. Please check for that before posting an answer, thanks. –  Jun 02 '21 at 10:17
1

this statement

if (dolphinsAverage || koalasAverage < 100)

resolve as

if (true || koalasAverage < 100)

what you need is

if (dolphinsAverage < 100 || koalasAverage < 100)
Amir Saadallah
  • 668
  • 1
  • 8
  • 19
  • This is one of the most common beginner's mistakes in programming and guaranteed to be a duplicate. Please check for that before posting an answer, thanks. –  Jun 02 '21 at 10:17
  • what do u mean? can u explain the mistake? – Amir Saadallah Jun 02 '21 at 10:20
  • If you come across a question that has an easy and obvious answer, chances are it has been asked before. On Stackoverflow you're not supposed to answer the same basic questions over and over again; instead you should flag the question as duplicate to send the OP to the existing answer. Check the blue box at the top of the page. –  Jun 02 '21 at 10:33
0

your if statement should be if (dolphinsAverage < 100 || koalasAverage < 100) {

Ayudh
  • 1,673
  • 1
  • 22
  • 55
0

const dolphinsScores = 97 + 112 + 101;
const dolphinsAverage = dolphinsScores / 3;

const koalasScores = 109 + 95 + 123;
const koalasAverage = koalasScores / 3;

console.log(dolphinsAverage);
console.log(koalasAverage);

if ((dolphinsAverage < 100) || (koalasAverage < 100)) {
    console.log('One of the team is already lost')
} else if (dolphinsAverage > koalasAverage) {
    console.log('Team Dolphins are the winner of the competition! ');
} else if (koalasAverage > dolphinsAverage) {
    console.log('Team Koalas are the winner of the competition! ');
}
Nikita Mazur
  • 1,602
  • 1
  • 5
  • 14
  • This is one of the most common beginner's mistakes in programming and guaranteed to be a duplicate. Please check for that before posting an answer, thanks. –  Jun 02 '21 at 10:17