代码只是没有执行,可能有语法问题或我可能完全错误

时间:2014-11-29 12:18:09

标签: javascript jquery

所以我有一个简单的项目 - 创建一个Hangman游戏。我有一个令人难以置信的基本刽子手游戏,只有一个单词,没有使用数组,编码和工作。添加加性猜测和随机化单词的数组后,它就不再有效了。

据我所知,它不再选择一个单词(取代字母的破折号现在只显示一个破折号,之前没有数组时我有一个预设单词,它显示每个字母的破折号)因此所有猜测都是错误的。其次,尽管所有这些猜测都是错误的,但没有一个被计算在内。

JSfiddle - http://jsfiddle.net/7jo8w1zw/

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>

jquery -

function hangman(word) {
    var trys = 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]);
    });
});

/*Createa 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)
*/

我最后投入了我的指示,以防有人关心阅读。否则,就我所知,我没有得到任何错误(我现在还不太了解萤火虫),它只是没有做我希望的事情。

提前谢谢你。像往常一样,你的帮助非常宝贵!

1 个答案:

答案 0 :(得分:2)

you wrote hangman('options'[random]);

选项是一个var,所以它不应该在引号之间。 它现在正在做的是从字符串'option'中取一个随机字符

也猜测没有在hangman函数中定义。