1 回答
TA贡献1784条经验 获得超7个赞
每次分数变化时,您都需要检查条件。为此,将您的 if 语句放在一个单独的函数中,并从有人得分时触发的函数中调用它。我创建小函数是因为,在我看来,这是最佳实践,但对于这么小的操作来说并不是必需的。
let pScore = 0;
let cScore = 0;
let user = ["Player", "Computer"];
let isGameOver = (score) => {
if (pScore === 10 || cScore === 10) {
return true;
}
return false;
}
function gameOver() {
let winner = pScore === 10 ? user[0] : user[1];
console.log(winner);
}
function theFunctionThatChangesTheScores() {
// after the code that changes the score
if ( isGameOver() ) {
// you can code in this block, but ideally.
// create another function and call it:
return gameOver();
}
return console.log("game is still on");
}
theFunctionThatChangesTheScores();
添加回答
举报