最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught Reference Error:_______ is not defined - Stack Overflow

programmeradmin7浏览0评论

I'm a beginner with limited knowledge of both Javascript and html, sorry if my questions are very basic. I'm trying to write a basic webpage that will do something to an array using Javascript when a button is clicked, then print the result to the page.

<!DOCTYPE html>
//error here
<html>
<head>
<title>Thursday's Homework</title>
<script>
    var problem1 = function() 
    {for (var i = 0; i <= 50; i += 5) {
        document.write(i + " ");
        }
    }   
    var myArray = []
    var problem2 = function()
        {for (var i = 0; i <= 9; ++i){
            myArray[i]
            }
        document.write(myArray)     
</script>
</head>
<body>
<center><h2><b> My Homework for Thursday<b></h2></center>
<p>This is my code for Problem 1:</p>
<input type="button" onclick = "problem1()" value="Does it work?">
//error here
<p>This is my code for Problem 2:</p>
<input type="button" onclick = "problem2()" value="Does it work?">
//error here
</body>
</html>

I'm getting three error messages when I debug in Chrome using developer tools: After line 1: Uncaught SyntaxError: Unexpected end of input After line 22(the first button): Uncaught ReferenceError: problem1 is not defined After line 24(the second button): Uncaught Reference Error: problem2 is not defined

I'm also wondering if there's a better way to display the outputs than document.write because that makes it so that the page needs to be reloaded each time to see the next result.

I'm new, so my apologies if this is pletely the wrong way to create this whole thing. If that's the case, just point me in the right direction.

UPDATE: I've fixed the syntax errors and it's running fine. I'm still wondering about a better way to display my result that document.write. I'm not too knowledgeable, so if you have a suggestion, could you give me an idea of how to implement it?

I'm a beginner with limited knowledge of both Javascript and html, sorry if my questions are very basic. I'm trying to write a basic webpage that will do something to an array using Javascript when a button is clicked, then print the result to the page.

<!DOCTYPE html>
//error here
<html>
<head>
<title>Thursday's Homework</title>
<script>
    var problem1 = function() 
    {for (var i = 0; i <= 50; i += 5) {
        document.write(i + " ");
        }
    }   
    var myArray = []
    var problem2 = function()
        {for (var i = 0; i <= 9; ++i){
            myArray[i]
            }
        document.write(myArray)     
</script>
</head>
<body>
<center><h2><b> My Homework for Thursday<b></h2></center>
<p>This is my code for Problem 1:</p>
<input type="button" onclick = "problem1()" value="Does it work?">
//error here
<p>This is my code for Problem 2:</p>
<input type="button" onclick = "problem2()" value="Does it work?">
//error here
</body>
</html>

I'm getting three error messages when I debug in Chrome using developer tools: After line 1: Uncaught SyntaxError: Unexpected end of input After line 22(the first button): Uncaught ReferenceError: problem1 is not defined After line 24(the second button): Uncaught Reference Error: problem2 is not defined

I'm also wondering if there's a better way to display the outputs than document.write because that makes it so that the page needs to be reloaded each time to see the next result.

I'm new, so my apologies if this is pletely the wrong way to create this whole thing. If that's the case, just point me in the right direction.

UPDATE: I've fixed the syntax errors and it's running fine. I'm still wondering about a better way to display my result that document.write. I'm not too knowledgeable, so if you have a suggestion, could you give me an idea of how to implement it?

Share Improve this question edited May 23, 2013 at 2:30 user2411787 asked May 23, 2013 at 2:19 user2411787user2411787 311 gold badge1 silver badge3 bronze badges 1
  • 4 you're not closing the problem2 function – Kai Qing Commented May 23, 2013 at 2:21
Add a ment  | 

4 Answers 4

Reset to default 2

You are missing a closing } after problem2.

That is the most mon cause for this kind of error. The JS interpreter breaks since the function body is not properly closed, stops parsing. Then something else tries to reference a variable/function that failed to get parsed and triggers the Reference Error.

I can see a few issues.

  1. You are missing a closing } after problem2.
  2. You have spaces around your = for onclick =.
  3. You're not actually doing anything with myArray. That does not seem intentional.
  4. Your code formatting is unclear and that can lead to difficulties later (and I think this, in part, caused the bug). Unless you have a particularly good reason you should avoid things like {for. Instead add a newline after the {.
  1. close the brace of problem2();
  2. instead of document.write(); u can specify a id like

    or

and in the js use

document.getElementById("abc").innerHTML=i+" ";

for better and safer coding use jsLint ..

As for you second question, you can find a lot of helpful extensions for both Chrome and Firefox that can satisfy you for debugging.

Chrome has internal tools such as "Web Console" that allow you to watch variables for exemple.

发布评论

评论列表(0)

  1. 暂无评论