我的代码中有两个不同的功能不起作用

时间:2014-11-30 05:29:18

标签: javascript jquery

第一个不起作用的功能是我的信件不被"检查"正确。我的意思是当你运行程序时,出现完整的字母并出现一些破折号(破折号表示单词中的字母数。)然后单击字母来猜测单词中的字母。观察到的结果是只检查单词的第一个字母是否正确,所有其他字母都是错误的。结果应该是所选择的任何一个字母,如果它出现在单词中,应该是否正确。

第二个不起作用的功能是我的猜测计数器(变量" trys"。)结果应该计算每个错误的猜测,并且在六个不正确的猜测中,它会发出一个警告,你输了并重置。注意:我目前没有编码来重置整个刽子手,只有猜测。在继续使用更多代码之前尝试解决这个问题。目前,观察到的结果是它似乎没有计算,或根本没有出现。这可能是由于前面提到的代码将一切都视为错误,因此它是问题二。修复问题可以解决问题二。虽然我显然不知道,但我仍然非常了解Javascript。

像往常一样,提前谢谢你。如果您有任何问题,我将会澄清一些事情。我还将链接到您,家庭作业分配信息:

/*Create a web page with the game Hangman, in which the user guesses letters in a hidden word. 
Create an array with at least a dozen words and pick one at random each time the game is started. 
Display a dash for each missing letter. Allow the user to guess letters continuously 
(up to 6 guesses) until all the letters in the word are correctly guessed. 
As the user enters each guess, display the word again, filling in the guess if it was correct. 
For example, if the hidden word is “ computer”, first display --------. 
After the user guesses “ p”, the display becomes ---p----. Make sure that when a user makes a 
correct guess, all the matching letters are filled in. For example, if the word is “ banana”, then
when the user guesses “ a”, all three “ a” characters are filled in. (25 points)
*/

JS小提琴 - http://jsfiddle.net/7jo8w1zw/2/

HTML -

<body>


<form id="form" name="form" method="post" action="">
<input type="button" id="but" value="Start"/>
<div id="hangman-jquery">
    <div id="word"></div>
    <div id="alpha"></div>
</div>
</form>

<div id="win">
</div>

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="hangman.js"></script>
</body>

Javascript:

function hangman(word) {
    var trys = 0;
    var guess = 0;
    var alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $.each(alpha.split(''), function(i, val) {
        $('#alpha').append($('<span class="guess">' + val + '</span>'));
    });
    $.each(word.split(''), function(i, val) {
        $('#word').append($('<span class="letter" letter="' + val + '">-</span>'));
    });
    $('.guess').click(function() {
        var count = $('#word [letter=' + $(this).text() + ']').each(function() {
            $(this).text($(this).attr('letter'));
        }).length;
        $(this).removeClass('guess').css('color', (count > 0 ? 'green' : 'red')).unbind('click');

        if (guess > 0) {
        $('#win').text("Correct Guess");
        } else if (guess < 0) {
        $(this).html(++trys);
        $('#win').text("You have tried to guess the word and failed " + trys + " times");
        }
        if (trys == 6) {
        alert("You have guessed six times, you lose");
        trys = 0;
        $("#win").text("");
        }
    });
}

$(document).ready(function() {
    $('#but').click(function() {
        var options = new Array("Dog", "Cat", "Bat", "Horse", "Tiger", "Lion", "Bear", "Liger", "Doom", "Spider", "Trees", "Laptop");
        var random = 1 + Math.floor(Math.random() * 12);
        hangman(options[random]);
    });
});

0 个答案:

没有答案
相关问题