简体   繁体   English

未捕获的typeError未定义不是函数

[英]Uncaught typeError undefined is not a function

I debugged my code plenty, but I am still having issues. 我调试了很多代码,但是仍然遇到问题。 This is a class assignment and I asked my professor and he is stunned as well. 这是一次课堂作业,我问了我的教授,他也很震惊。 I am certain that fixing this error will help me with my code, but what I got wrong I am not sure. 我确定修复此错误将对我的代码有帮助,但是我不确定我出错了。 Could you help? 你能帮忙吗? Here is the error 这是错误

在此处输入图片说明

Here my html code 这是我的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 这是我的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);
}

Where is my error? 我的错误在哪里?

This: 这个:

var userTrials = document.getElementbyId("userGuesses").innerText;

gets the text of the "userGuesses" element as a string , but here: 字符串形式获取“ userGuesses”元素的文本,但是在这里:

userTrials.appendChild(divisor);

your code treats it as if it were a DOM node. 您的代码将其视为DOM节点。 Because there's no "appendChild" property on a String instance, it's undefined , and that explains the error — you're trying to make a function call but the function reference is undefined instead. 因为String实例上没有“ appendChild”属性,所以它是undefined ,并且可以解释该错误-您正在尝试进行函数调用,但函数引用却是undefined

edit — oh also that typo in "getElementById()" is also important, as noted by crclayton in a comment. 编辑 —哦,正如crclayton在评论中指出的那样,“ getElementById()”中的错字也很重要。 I missed that one :) The general trick to dealing with the 我错过了一个:)处理

undefined is not a function 未定义不是函数

error is to try and find a place where your code might come up with undefined instead of a reference to a function. 错误是尝试查找代码可能undefined而不是引用函数的地方。 The primary causes, in my experience, are: 根据我的经验,主要原因是:

  • Typos, like the "getElementbyId" thing. 错字,就像“ getElementbyId”一样。 If the function name is misspelled, JavaScript won't understand that and so it can't give a better error. 如果函数名称拼写错误,JavaScript将无法理解,因此不会给出更好的错误。
  • Unsatisfied assumptions about what a variable or property refers to, as in the userTrials variable. 关于变量或属性指的是不满意的假设,例如userTrials变量。
  • Assumption that some function returns a reference to a function, when it in fact (might) return undefined 假设某个函数实际上返回了undefined (可能)返回了对该函数的引用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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