-3

我在 javascript 方面或多或少是新手,我正在尝试制作一些有趣的程序,我希望它询问你的名字,然后告诉你一些事情作为回报。这是代码:

var name = (prompt("What is your name? Capitalization matters!"));

if (name === Random name) {

    return("Random name :D, I think you already know who sent you this, but if not it's me  :D.");
}
else {

return("Is that really your name? If so, that shows how popular I am haha. If that's not your name, LIAR!");
    }
4

2 回答 2

4

你只能return从一个函数中,你发布的不是一个函数。您可以alert改用:

var name = prompt("What is your name? Capitalization matters!");
if (name === "John") {
    alert("Random name :D, I think you already know who sent you this, but if not it's me  :D.");
} else {
    alert("Is that really your name? If so, that shows how popular I am haha. If that's not your name, LIAR!");
}

如果你想让它成为一个函数:

function checkName() {
    var name = prompt("What is your name? Capitalization matters!");
    if (name === "John") {
        return "Random name :D, I think you already know who sent you this, but if not it's me  :D.";
    } else {
        return "Is that really your name? If so, that shows how popular I am haha. If that's not your name, LIAR!";
    }
}
alert(checkName());
于 2013-05-25T19:42:34.970 回答
0

如果要检查用户是否输入了“随机名称”,则需要像打印时那样将字符串括在引号中:

var name = (prompt("What is your name? Capitalization matters!"));    
if (name === "Random name") {
    return("Random name :D, I think you already know who sent you this, but if not it's me  :D.");
} else {   
    return("Is that really your name? If so, that shows how popular I am haha. If that's not your name, LIAR!");
}

只是我的猜测,寿。

于 2013-05-25T19:39:29.007 回答