gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:10:06 25 4
gpt4 key购买 nike

所以我有一个简单的项目 - 创建 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)
*/

我在最后加入了我的说明,以防有人愿意阅读它。否则,据我所知,我没有收到任何错误(我还不太了解 Firebug ),它只是没有按照我希望的那样去做。

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

最佳答案

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

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

guess 也没有在 hangman 函数中定义。

关于javascript - 代码没有执行,可能有语法问题,或者我可能完全错了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27202122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com