缺少半冒号的语法错误

时间:2016-03-29 10:51:02

标签: javascript

为什么我不断收到此错误代码的任何想法:

SyntaxError: missing ; after for-loop condition.

我已经尝试在最后添加一个分号。 这是我的代码:

        var numberOfQuestions = 3;

    for(i = 0; i <=3 numberOfQuestions; numberOfQuestions--) {
        if (numberOfQuestions == 3) {
            var adjective = prompt('Please type an adjective ' + '3 questions left');
        }
        if (numberOfQuestions == 2) {
            var verb = prompt('Please type a verb ' + '2 questions left');
        }
        if (numberOfQuestions == 1) {
            var noun = prompt('Please type a noun ' + '1 question left');
        }
    };
        alert('All done. Ready for the message?');
        document.write("There was once a " + adjective + " programmer who wanted to use Javascript to " + verb + " the " + noun);

3 个答案:

答案 0 :(得分:2)

替换

i <=3 numberOfQuestions

通过

i <=numberOfQuestions ; //
在你的for循环中间条件

答案 1 :(得分:2)

这里有好几件事。首先,正如其他人指出你的循环在这里有一个无效值:

for(i = 0; i <=3 numberOfQuestions; numberOfQuestions--) {
               ^^^

我们假设你想从numberOfQuestions开始并倒计时。在这种情况下,您需要将i设置为numberOfQuestions,然后减少循环中i的值:

for (var i = numberOfQuestions; i >= 1; i--) {

这意味着&#34;将i设置为numberOfQuestions。当i大于或等于1时,运行循环。执行循环后,使用1(i)&#34;。

减少i--

也没有理由使用

if (a === 1) {
}
if (a === 2) {
}

如果a为1则永远不会是2,因此没有理由检查所有值。相反,您应该使用else if

第三,你应该知道变量提升意味着你可能认为你在循环中声明你的变量,但你真的不是。这会让你后悔,所以先声明它们。最终结果将是代码如下:

var numberOfQuestions = 3;
var i, adjective, verb, noun;

for (i = numberOfQuestions; i >= 1; i--) {
    if (i === 3) {
        adjective = prompt('Please type an adjective ' + '3 questions left');
    } else if (i === 2) {
        verb = prompt('Please type a verb ' + '2 questions left');
    } else if (i === 1) {
        noun = prompt('Please type a noun ' + '1 question left');
    }
};

alert('All done. Ready for the message?');
document.write("There was once a " + adjective + " programmer who wanted to use Javascript to " + verb + " the " + noun);

如果您只想更改numberOfQuestions,我建议改为while循环:

var numberOfQuestions = 3;
var adjective, verb, noun;

while (numberOfQuestions > 0) {
    if (numberOfQuestions === 3) {
        adjective = prompt('Please type an adjective ' + '3 questions left');
    } else if (numberOfQuestions === 2) {
        verb = prompt('Please type a verb ' + '2 questions left');
    } else if (numberOfQuestions === 1) {
        noun = prompt('Please type a noun ' + '1 question left');
    }

    numberOfQuestions--;
};

那说 - 为什么你甚至使用循环?您需要所有值,因此请直接使用prompt

var adjective = prompt('Please type an adjective - 3 questions left');
var verb = prompt('Please type a verb - 2 questions left');
var noun = prompt('Please type a noun - 1 question left');

alert('All done. Ready for the message?');
document.write("There was once a " + adjective + " programmer who wanted to use Javascript to " + verb + " the " + noun);

答案 2 :(得分:0)

TA.ColumnC IS NULL

试试这个而不是你的for循环