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

Code just isn't performing, might have a syntax issue or i could be completely wrong

本文关键字:或者 错了 问题 语法 可能有 执行 代码      更新时间:2023-09-26

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

据我所知,它不再选择一个单词(代替字母的破折号现在只显示一个破折号,

以前没有数组并且我有一个预设的单词,它为每个字母显示破折号),因此所有的猜测都是错误的。其次,尽管所有这些猜测都是错误的,但没有一个被计算在内。

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

我在最后扔了我的指示,以防有人愿意阅读它。否则,据我所知,我没有收到任何错误(我还不擅长理解Firebug),它只是没有做我希望它做的事情。

提前谢谢你。您一如既往的帮助是无价的!

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

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

此外,猜测也没有在刽子手函数中定义。