Hello I am learning JavaScript and I have a question, I have made simple algorithm to check "if something". My question is about this line if(dolphins && koalas > minimumScore)
. It seems to me illogical, but it works in a way I want. Because in beginning I wanted to check if dolphins or koalas > minimumScore (So I used ||
). But when I set both teams to value under 100 it kept going to the next if
block and else if
but not to else statement. So I had to use && and it works, it goes to the else if both teams are under 100 and goes to the next 'if' when at least one team is higher than 100.
let dolphins = 11;
let koalas = 11
const minimumScore = 100
if(dolphins && koalas > minimumScore){
if(koalas > dolphins){
console.log(`Koalas won! {$koalas}`)
} else if(koalas===dolphins){
console.log('draw')
} else {
console.log('dolphins won!')
}
} else{
console.log('no team won')
}
dolphins
are truthy, andkoalas
are overminimumScore
… You needif (dolphins > minimumScore || koalas > minimumScore)
.