如何正确使用var

时间:2013-05-14 07:23:46

标签: javascript prompt var

我刚开始在codecademy上学习Javascript,不知道这段代码有什么问题。

var feedback = prompt("Rate this game out of 10");
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!")
};

它说变量有问题。这可能是一个太容易的问题,但我不知道如何弄明白。

3 个答案:

答案 0 :(得分:3)

在javascript中,不允许在字符串中有一个难点。

这一行:

console.log("I slaved away at this game and you gave me that score?! The nerve! Just you          
wait!")};

应该是:

console.log("I slaved away at this game and you gave me that score?! The nerve! Just you wait!")};

答案 1 :(得分:2)

您的代码适用于我。 唯一的问题是第二个console.log语句中的换行符,但我不知道这是否只是您在发布时发生的复制和粘贴错误。

答案 2 :(得分:0)

Klaasman指出的问题是,你有一个换行符。

console.log("This is a very long string
that went more than one line long");

如果你想要字符串多行,你必须使用\来告诉JavaScript“字符串在下一行继续”

console.log("This is a very long string \
that went more than one line long");

这样你可以像这样编写相同的字符串:

console.log("This \
is \
a \
very \
long \ 
string");

您还忘记了最后一个console.log语句中的分号。