【问题标题】:Correct use of var x=prompt()?正确使用 var x=prompt()?
【发布时间】:2015-10-20 00:55:36
【问题描述】:

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

这就是要点。 if else 语句和这里的大部分函数代码直接来自我的导师,但我不确定我需要什么才能让提示显示“你是对的!”。我尝试了各种方法,但每次我在提示中输入正确答案以进行测试时,都会出现 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 ++)
}

【问题讨论】:

    标签: javascript variables if-statement var prompt


    【解决方案1】:

    在提示之后,您有一些看起来像是在尝试功能代码块的东西,但它不应该存在。它被解释为内联代码块,因此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 函数中的代码,这样你就可以通过游戏状态。

    【讨论】:

    • 非常感谢 Guffa。我实际上是五周后的一年级媒体学生。我确信在可预见的未来我的代码中会有漏洞,但在大多数情况下,我觉得我已经能够通过试用和错误。这个不多说哈哈。到达那里!
    猜你喜欢
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 2011-03-06
    • 2018-09-25
    • 1970-01-01
    相关资源
    最近更新 更多