英文:
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
已更改常量变量dolphinScore
和koalaScore
为普通声明,并且执行成功。以下是我新的代码,它是否是解决此问题的正确方式?
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");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论