Else if语句未执行,它应该在这种情况下使用console.log打印最后一个语句。

huangapple go评论42阅读模式
英文:

Else if statement not executing , its supposed to console.log the last statement in this case

问题

// 最后一个 else if 中是否需要添加括号以设置优先级?

const dolphinScore = 88 + 91 + 110 / 3;
console.log(dolphinScore);
const koalaScore = 88 + 91 + 110 / 3;
console.log(koalaScore);

// 88 + 91 + 110 / 3

const minScore = 100;

if (dolphinScore > koalaScore && minScore) {
console.log(The dolphins win with a score ${dolphinScore})
} else if (koalaScore > dolphinScore && minScore) {
console.log(The koalas win with a score of ${koalaScore})
} else if (dolphinScore == koalaScore && >= minScore) {
console.log("This is a draw, no one wins");
}

// 预期 'else if' 会执行,但它似乎不起作用,请对我宽容一些,我正在重新学习基础知识!

'else if' 语句没有执行,它应该在这种情况下输出最后一个语句,但似乎好像 '(dolphinScore == koalaScore && >= minScore)' 不为真?

英文:
// Do I have to place parentheses somewhere in the last else if to prioritize?

const dolphinScore = 88 + 91 + 110 / 3;
console.log(dolphinScore);
const koalaScore = 88 + 91 + 110 / 3;
console.log(koalaScore);

//88 + 91 + 110 / 3

const minScore = 100;

if (dolphinScore > koalaScore && minScore) {
    console.log(`The dolphins win with a score ${dolphinScore}`)
} else if (koalaScore > dolphinScore && minScore) {
    console.log(`The koalas win with a score of ${koalaScore}`)
} else if (dolphinScore == koalaScore && >= minScore) {
    console.log("This is a draw, no one wins");
}

Expected the else if to execute but it isn't working go easy on me I'm re leaning the basics!

Else if statement not executing, it's supposed to console.log the last statement in this case but its acting as if the (dolphinScore == koalaScore && >= minScore) isn't true?

答案1

得分: 1

我认为问题出在这一行:

if (dolphinScore > koalaScore && minScore)

应该是:

if (dolphinScore > koalaScore && dolphinsScore > minScore)

&& 操作必须在两个布尔语句之间。

英文:

I think that the problem lies in this line:

if (dolphinScore > koalaScore && minScore)

It should be:

if (dolphinScore > koalaScore && dolphinsScore > minScore)

The && operation must be between 2 boolean statement.

答案2

得分: -1

已更改常量变量dolphinScorekoalaScore为普通声明,并且执行成功。以下是我新的代码,它是否是解决此问题的正确方式?

let dolphinScore = 88 + 91 + 110 / 3;
console.log(dolphinScore);
let koalaScore = 88 + 91 + 110 / 3;
console.log(koalaScore);

// 88 + 91 + 110 / 3

const minScore = 100;

if (dolphinScore > koalaScore && minScore) {
    console.log(`The dolphins win with a score ${dolphinScore}`)
} else if (koalaScore > dolphinScore && minScore) {
    console.log(`The koalas win with a score of ${koalaScore}`)
} else if (dolphinScore == koalaScore || dolphinScore, koalaScore = minScore) {
    console.log("This is a draw, no one wins");
}
英文:

Changed the constant variables dophinsScore & koalaScore to just regular declarations and it executed here's my new code that works,
could someone let me know if this is the proper way to solve this issue?

let dolphinScore = 88 + 91 + 110 / 3;
console.log(dolphinScore);
let koalaScore = 88 + 91 + 110 / 3;
console.log(koalaScore);

//88 + 91 + 110 / 3

const minScore = 100;


if (dolphinScore > koalaScore && minScore) {
    console.log(`The dolphins win with a score ${dolphinScore}`)
} else if (koalaScore > dolphinScore && minScore) {
    console.log(`The koalas win with a score of ${koalaScore}`)
} else if (dolphinScore == koalaScore || dolphinScore, koalaScore = minScore) {
    console.log("This is a draw, no one wins");
}

huangapple
  • 本文由 发表于 2023年2月16日 11:43:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467646.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定