Hi everyone,

I been looking around how to solve this problem but I am stuck. I asked my professor and he is stuck. Can someone look at the code and see why I am getting this error?

Thank you

Click Here

Here is my HTML

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Magic Word - Sample</title>
    <script src="magicWord.js"></script>
</head>
<body>
    <div>
        <h2 align="center">WELCOME TO MAGIC WORD!</h2>
        <h3>Please enter your guess on the space below:</h3>
        <div>
            <input type="text" id="guestGuess" />
            <input type="submit" id="submitButton" onclick="javascript:enteredGuess();" />
        </div>
    </div>
    <div>
        <h3>Your guesses so far are: </h3>
        <div id="userGuesses"></div>
        <div>You have guessed: <span id="userAttempts"></span> times</div>
    </div>
</body>
</html>

Here is my Javascript

var numOfAttempts = 5;
var numOfGuesses = 0;
var magicWord = "Just";

function guessesUsed(attemptsTaken) {
    var numGuessesLeft = document.getElementById("userAttempts");
    numGuessesLeft.innerHTML = attemptsTaken;
}

//function guessesLeft(attemptsLeft) {
//  var numGuessesUsed = document.getElementById("guessesLeft");
//  numGuessesUsed.innerHTML = attemptsLeft;    
//}

function listWordsUsed(wordUsed) {
    var userTrials = document.getElementbyId("userGuesses").innerText;
    var divisor = document.createElement("div");
    divisor.innerHTML = "<div id=" + wordUsed + ">" + wordUsed + "</div>";
    userTrials.appendChild(divisor);
    return;
}

function enteredGuess() 
{
    //A user will enter a word and the word will be checked against the magic word
    //var enterGuess = prompt("What is your guess?", "Enter your guess here");
    var theGuess = document.getElementById("guestGuess");
    var magicWordResult;

    // Used lower case for both instances to compare both words
    if (theGuess.value.toLowerCase() == magicWord.toLowerCase()) 
    {
        //The user guesses the correct word
        magicWordResult = "Your guess was" + theGuess + "! Nice going!";
        //document.writeln("Your guess was: " + guestGuess + "\nand the magic word is " + magicWord);
    }
    else  //The user guesses wrong
    {
        //When user gets it wrong increase by one the numOfGuesses
        numOfGuesses ++;
        magicWordResult = "Your guess was" + theGuess + ". Nice try, but that's wrong, try again. \nYou have used " + numOfGuesses + " guesses.";

        // Subtract the amount of guesses from the amount of attempts
        var guessesLeft = numOfAttempts - numOfGuesses;

        if (numOfGuesses == numOfAttempts)
        {
            // When the user has reached all its attemps
            magicWordResult = "Sorry, you ran out of guesses. The magic word was " + magicWord + ".";
        }
        else
        {
            magicWordResult = "You still have " + guessesLeft + " guesses left.";
        }

        //To show the number of guesses the user has used
        guessesUsed(numOfGuesses);

        //To show the number of guesses the user has left
        //guessesLeft(guessesLeft);

        //Creates a list of the words used by the user
        listWordsUsed(theGuess);

    }

    // Display in an alert mode and show how the user is doing
    alert(magicWordResult);
}

Thanks for any help guys.

Recommended Answers

All 4 Replies

You have two errors in your code. Change line 20 to:
var userTrials = document.getElementById("userGuesses");

Firstly, its getElementById, camelcase for all words. Secondly, you don't want the innerText portion of the userGuesses div, you just want the div itself.
Thirdy, find a new professor.

Thank you for your reply. I have one question then, how can I append the word entered by the user to the HTML using the <div> tag? Is this possible?

Thirdy, find a new professor.

I agree with that. i consider myself a novice at javascript and its fairly easy to find the issues with your sample code. I've taught for many years in the classroom and I cant imagine a professor/instructor not being able to help you with this issue.

In any event, on line 63...listWordsUsed(theGuess);, you are sending the object not the value. send the value to your function instead.

 listWordsUsed(theGuess.value);
var divisor = document.createElement("div");
divisor.innerHTML = "<div id=" + wordUsed + ">" + wordUsed + "</div>";

That's wrong because you are unneccessarily creating a nested div element. If you want to append content only, simply add to its own value...

var divGuess = document.getElementById("userGuesses")
divGuess.innerHTML = divGuess.innerHTML + "<br>"+worduse
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.