正确使用var x = prompt()?

时间:2015-10-20 00:55:36

标签: javascript variables if-statement var prompt

我试图使用严格的提示创建一个简单的游戏。游戏将通过提示onclick向您提问。提示会询问您一个问题,您将在空白字段中输入您的猜测'。如果你猜对了,它会再次提示"你赢了!",如果你猜错了,它会提示"再试一次!"在同一提示中包含以下问题。有五个猜测,如果你不在最后猜测,会出现一个提示"你输了!"

这是要点。这里的if else语句和大部分功能代码直接来自我的导师,但是我不确定我需要什么才能提示出来"你是对的!&# 34 ;.我尝试了各种各样的东西,但每次我在提示中键入正确答案进行测试时,Null就会出现。

如果我能通过获得正确的答案来弄清楚如何让游戏结束,那么我认为我应该能够弄清楚如何重复问题提示功能。



<!doctype html>
<html>
<head>
<title>Camping Trip!</title>
<meta charset="utf-8">
<link type="style" src="style.css">
</head>
<body>

<br><br><br><br><br><br><br>
<center><h1>"I'm going on a camping trip..."</h1></center>
<br>
<!-- This is the Start Game button that begins the game-->

<center>  
    <button onclick="gamePlay()">Start Game!</button>
</center>
    
<!-- This is the Tutorial button that shows the rules-->

<br>

<center>
    <button id="show"onclick="startTutorial()">Show Tutorial</button>
        <p id="rules"></p>
</center>
<!-- This is the Hide Tutorial Button-->    
<center>
    <button id="hide">Hide Tutorial</button>
</center>

<!-- This is the Camping Trip picture-->
    
<center>
<img class="irc_mi" style="margin-top: 141px;" src="http://i10.photobucket.com/albums/a146/dizzybint78/tent.gif" width="600" height="333">
</center>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
    
alert("Welcome to the Caming Trip game!");

function startTutorial() {
    document.getElementById("rules").innerHTML = "<strong>Rules:</strong><br><br>The rules are simple! Initially, you will be given a clue, and an attempt to answer. There will be five clues. All five clues will follow a certain pattern, trait, or category. It's up to you to find out what that is!";
}
    
$(document).ready(function(){
    $("#hide").click(function(){
        $("p").hide();
    });
        $("#show").click(function(){
        $("p").show();
    });
});
    
var gameState = 0;
    
function gamePlay() {
if (gameState == 0) { 
gameQ1(); 
}
else if (gameState == 1) {
gameQ2();
}
else if (gameState == 2) {
gameQ3();
}
else if (gameState == 3) {
gameQ4();
}
else if (gameState == 4) {
gameQ5();
}
else if (gameState == 5) {
prompt("Sorry! Try Again!");
 }
}

function gameQ1() {
var answer = "fruit"
var guess=prompt("I'm going on a camping trip, and I'm bringing an Apple.\n\nAnswer:"); {
return prompt(guess);
}
if (prompt.guess == answer) {
alert("You've Won!");
document.refresh;
}
else (gameState ++)
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

你有一些看起来像是在提示后尝试功能代码块的东西,它不应该存在。它被解释为内联代码块,因此return将退出gameQ1函数。

var guess=prompt("I'm going on a camping trip, and I'm bringing an Apple.\n\nAnswer:"); {
return prompt(guess);
}

应该只是:

var guess=prompt("I'm going on a camping trip, and I'm bringing an Apple.\n\nAnswer:");

当您检查结果时,您正在使用变量,就好像它是prompt函数对象的属性一样。这只是一个变量。

if (prompt.guess == answer) {

应该是:

if (guess == answer) {

当您尝试重新加载页面时,您只是访问该功能,而不是调用它。

document.refresh;

应该是:

document.refresh();

这应该让你开始。我可以告诉你,你需要在某处循环,以便一遍又一遍地运行gamePlay函数中的代码,以便你可以完成游戏状态。