0

我对 JavaScript 很陌生,所以如果我犯了一个愚蠢的错误,我深表歉意,但我在这里找不到错误。我正在创建一个“选择你自己的冒险”类型的游戏,它进展顺利,直到我添加了一些 do/while 循环和 if/else 语句。

我已经测试了一段时间,通过消除过程,我发现只有当我有这一行时才会出现错误:

if (worst === (options[0] || options[1])) {
    options.splice(2, 0, best);

但对于我的生活,我找不到错误。

如果有帮助,这是整个脚本:

var options = ["STRENGTH", "SPEED", "SMARTS"];
alert("Before we begin our journey, let's learn a little bit  about you.");
var user = prompt("What is your name?").toUpperCase();
do {
var bestLoop = false;
var best = prompt("So " + user + ", what is your greatest ability, STRENGTH, SPEED, or SMARTS?").toUpperCase();
if (best === ("STRENGTH" || "SPEED" || "SMARTS")) {
        var offset = options.indexOf(best);
        if (offset != -1) {
            options.splice(offset, 1);
        }   
    } else {
    alert("Please choose either STRENGTH, SPEED, or SMARTS as your greatest ability.");
        bestLoop = true;
    }
} while (bestLoop);
alert("Great! So " + best + " is yor greatest ability.");
do {
var worstLoop = false;
var worst = prompt("So which is your greatest weakness, " + options[0] + " or " + options[1] + "?").toUpperCase();
if (worst === (options[0] || options[1])) {
    options.splice(2, 0, best);
} else {
    alert("Please choose either " + options[0] + " or " + options[1] " as your greatest weakness.");
    worstLoop = true;
}
} while (worstLoop);
alert("Great. So your name is " + user + ", " + best + " is your greatest ability, and " + worst + " is your greatest weakness.");

再次,如果我忽略了某些事情,我深表歉意,但这已经让我发疯了几个小时,我似乎无法找到问题所在。

4

1 回答 1

0

您在行中缺少一个“+”

alert("Please choose either " + options[0] + " or " + options[1]" as your greatest weakness.");

所以应该是:

alert("Please choose either " + options[0] + " or " + options[1] + " as your greatest weakness.");

这让翻译感到困惑。

建议:如果您能找到配置文本编辑器的方法,请将其与 jslint 或 jshint 一起使用。通过为您找到缺少的运算符或括号,这可以节省大量时间。

于 2013-06-29T23:14:19.947 回答