javascript上出现错误.创造一个文字游戏

getting an error on javascript. creating a word game

本文关键字:一个 文字游戏 创造 错误 javascript      更新时间:2023-09-26
var feedback = prompt("rate the game 1-10");
if (feedback < 8) {
  console.log("This is just the beginning of my game empire. Stay tuned for more!");
} else (feedback > 8) {
  console.log("I slaved away at this game and you gave me that score?! The nerve! Just you wait!");
}

else后面不应该有条件:)而且你的比较似乎是落后的;)我认为通常是10分的高分!

应该是else if(feedback > 8) !

如果你想在If -then-else块中测试另一个条件,你需要另一个If。

if (feedback < 8) {
  console.log("This is just the beginning of my game empire. Stay tuned for more!");
} else {
  if (feedback > 8) {
    console.log("I slaved away at this game and you gave me that score?! The nerve! Just you wait!");
  } else {
    // What about feedback == 8?
  }
}

您需要添加另一个if或删除第二个条件。此外,检查插入值也不错。

if (feedback < 8) {/* Stuff */}
else {/* other stuff */}

if (feedback < 8) {/* Stuff */}
else if (feedback > 8) {/* other stuff */}

还应该检查输入值

function feedback () {
    checkFeedback(prompt('rate the game 1-10'))
}
function checkFeedback (fdb) {
    parsed = parseInt(fdb);
    if (typeof(parsed) != 'number' || parsed < 1 || parsed > 10) {
        alert('Number between 1 and 10 needed');
        feedback();
    }
    else logMessage(parsed);
}
function logMessage(feedback) {
    if (feedback<8)
        console.log('This is just the beginning of my game empire. Stay tuned for more!')
    else
        console.log('I slaved away at this game and you gave me that score?! The nerve! Just you wait!')
}
feedback();