Bummer! You must be logged in to access this page.

Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

I need help with my hang man code?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>hangman</title>
    <link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
    <h1>Hang man</h1>
    <p>Lives <span id="lives">5</span></p>
    <p id="word">_______</p>
    <p>Letters: <span id="letters"></span></p>
    <script src="assets/js/app.js"></script>
</body>
</html>

Having issues with matching letters

var words = ["hello", "hunger", "buy", "turkey"];
var choseAlphebet = [];
var index;
var chosenWord = []
var win = []
var index = Math.floor(Math.random() * 4)
var stringLenght = words[index]
var letters = document.querySelector("#letters");
var correct = document.querySelector("#word")
var lives = 5;
document.onkeyup = function(event){ 
    var keyup = String.fromCharCode(event.keyCode).toLowerCase();
    console.log(stringLenght)
    var split = stringLenght.split("")
    console.log(split)
    for(var i = 0; split.length > i; i++) {
        if(keyup == split[i]) {
            win.push(keyup);
            correct.innerHTML = win;
        } else {
            choseAlphebet.push(keyup);
            letters.innerHTML = choseAlphebet;
        } 
    }
    if(split.length === win.length) {
        split.length = []
        win.length = []
        alert("win")
    }
}

1 Answer

The code inside the for-loop is never executed

for(var i = 0; split.length > i; i++) {

should be:

for(var i = 0; split.length < i; i++) {