Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我的代码中没有两个不同的函数_Javascript_Jquery - Fatal编程技术网

Javascript 我的代码中没有两个不同的函数

Javascript 我的代码中没有两个不同的函数,javascript,jquery,Javascript,Jquery,第一个不起作用的功能是我的信件没有被正确检查。我的意思是,当你运行程序时,会出现完整的字母表,并且会出现一些破折号。破折号表示单词中的字母数。然后单击这些字母猜单词中的字母。观察到的结果是,只检查单词的第一个字母是否正确,而所有其他字母都是错误的。结果应该是,选择的任何字母,无论是否无序,如果在单词中,都应该显示为正确 第二个不起作用的函数是my guess计数器变量trys。结果应该统计每一个错误的猜测,并且在六个错误的猜测中,它会发出一个警报,提示您丢失并重置。注:我目前没有代码重置整个绞刑

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

第二个不起作用的函数是my guess计数器变量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小提琴-

HTML-

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]);
    });
});

你首先要检查的是你比较的字母的大小写。我不完全明白你在说什么。我检查了所有变量和id的大小写,以及调用。据我所知,他们似乎都很好。你能解释一下你指的是什么吗?他的意思是你使用的是大写字母所有字母都是大写的,但是你的选项数组包含小写字母的单词:a=A、 b=B etcOMG,你们太对了。打得好,我都没想过。让我现在去改变一下,看看情况如何。编辑:那个问题解决了,谢谢大家!
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]);
    });
});