简体   繁体   中英

Cant figure out why my div output isn't working

I have a basic hangman program. Click on the letter, it then tells you if its right or wrong by coloring the letter red or green. If its green, it replaces a dash in the hidden word with the letter. That part of my code works. The part that doesn't work is that I have an output section to a div that is supposed to tell you if you got the guess correct or not, and upon 6 failed guesses, you lose and it resets. It's currently not doing any of that. It's not even displaying anything in the div at all.

TY ahead of time. If you have questions about something I didn't explain well enough, ill be back in about 1 hour to answer them. Thanks!

Jsfiddle - http://jsfiddle.net/25b3fr4u/

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>

JS -

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

Code fixed : http://jsfiddle.net/25b3fr4u/1/ . The trys where not incremeted properly and you have to check the count instead of guess in your condition. guess was set to 0 and never use.

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 (count > 0) {
    $('#win').text("Correct Guess");
    } else if (!count > 0) {
    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("");
    }

I'm sure you can handle the rest. Have fun.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM