我对 JavaScript 相当陌生,并且使用 switch 语句做了一个非常基本的选择你自己的冒险游戏。当我尝试使用按钮调用我的函数时,它不成功。我的 JavaScript 就是这个。
function chooseGame(){
var troll = prompt("You are walking through a forest on your way to GameStop. You come across a troll guarding the only bridge to get there, he says if you pay him he will let you by. Do you PAY, KILL, or CHALLENGE HIM TO A GAME OF CHESS?").toUpperCase();
switch(troll){
case'PAY':
var money = prompt("Do you have money").toUpperCase();
var amount = prompt("IS it $50?").toUpperCase();
if (money && amount === "YES"){
console.log("You pay him and steal the game you wanted, get caught, and go to prison.");
}else{
console.log("He broke your legs and stole your shoes, not like you'd be needing them.");
}
break;
case'KILL':
var weapon = prompt("Do you have a weapon?").toUpperCase();
var friend = prompt("Do you have friends with you?").toUpperCase();
if (weapon || friend === "YES"){
console.log("You break his legs and take his money buying multiple games.");
}else{
console.log("He brings you back to his personal torture chamber and laughs at your pain for all eternity.");
}
break;
case'CHALLENGE HIM TO A GAME OF CHESS':
var clever = prompt("Are you clever?").toUpperCase();
var ability = prompt("Are you good at chess?").toUpperCase();
if (clever || ability === "YES"){
console.log("You beat him and go on your way to GameStop.");
}else{
console.log("You aren't clever or good at chess, then why did you challenge a troll to a game?");
}
break;
};
}
使用我的 HTML,它看起来像这样。
<html>
<head>
<Script language = "Javacript">
function chooseGame(){
var troll = prompt("You are walking through a forest on your way to GameStop. You come across a troll guarding the only bridge to get there, he says if you pay him he will let you by. Do you PAY, KILL, or CHALLENGE HIM TO A GAME OF CHESS?").toUpperCase();
switch(troll){
case'PAY':
var money = prompt("Do you have money").toUpperCase();
var amount = prompt("IS it $50?").toUpperCase();
if (money && amount === "YES"){
console.log("You pay him and steal the game you wanted, get caught, and go to prison.");
}else{
console.log("He broke your legs and stole your shoes, not like you'd be needing them.");
}
break;
case'KILL':
var weapon = prompt("Do you have a weapon?").toUpperCase();
var friend = prompt("Do you have friends with you?").toUpperCase();
if (weapon || friend === "YES"){
console.log("You break his legs and take his money buying multiple games.");
}else{
console.log("He brings you back to his personal torture chamber and laughs at your pain for all eternity.");
}
break;
case'CHALLENGE HIM TO A GAME OF CHESS':
var clever = prompt("Are you clever?").toUpperCase();
var ability = prompt("Are you good at chess?").toUpperCase();
if (clever || ability === "YES"){
console.log("You beat him and go on your way to GameStop.");
}else{
console.log("You aren't clever or good at chess, then why did you challenge a troll to a game?");
}
break;
};
}
</Script>
</head>
<body>
<button type="button" onclick = "myFunction();">Game Start</button>
</body>
</html>
当我以 .html 形式运行我的代码时,它会显示按钮,但在我单击它时不会执行任何操作。我能够使按钮发出警报,所以我相信这是我的代码没有调用我的函数的问题,如果我做错了什么,有人可以提醒我。