我是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.");
再次,如果有一些我忽略的事情,我道歉,但这让我疯狂了几个小时,我似乎无法找到问题。
答案 0 :(得分: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。通过为您找到缺少的运算符或括号,可以节省大量时间。