未被捕获的参考错误:未定义。

时间:2022-11-05 08:36:10

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.

我是一个初学者,对Javascript和html的知识都很有限,如果我的问题很基础的话,很抱歉。我试着写一个基本的网页,当一个按钮被点击时,它会用Javascript对数组做一些事情,然后把结果打印到页面上。

<!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

在使用developer tools调试Chrome时,我得到了三个错误消息:在第1行之后:未捕获的SyntaxError:第22行(第一个按钮)之后的输入意外结束:未捕获的ReferenceError:问题1在第24行(第二个按钮)之后没有定义:未捕获的引用错误:问题2没有定义。

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 completely 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?

更新:我修正了语法错误,它运行良好。我仍然在想一个更好的方法来显示我的结果,document。write。我不太了解,所以如果你有一个建议,你能告诉我如何实施吗?

4 个解决方案

#1


2  

You are missing a closing } after problem2.

问题2之后,你错过了一个结尾。

That is the most common 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.

这是造成这种错误的最常见原因。由于函数体未正确关闭,JS解释器会中断,停止解析。然后,其他一些东西试图引用一个变量/函数,该变量/函数未能被解析并触发引用错误。

#2


1  

I can see a few issues.

我能看到一些问题。

  1. You are missing a closing } after problem2.
  2. 问题2之后,你错过了一个结尾。
  3. You have spaces around your = for onclick =.
  4. 在= for onclick =中有空格。
  5. You're not actually doing anything with myArray. That does not seem intentional.
  6. 实际上你并没有使用myArray做任何事情。这似乎不是故意的。
  7. 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 {.
  8. 您的代码格式不清楚,这可能会导致以后的困难(我认为这部分是导致bug的原因)。除非你有一个特别好的理由,你应该避免像{这样的事情。改为在{。

#3


0  

  1. close the brace of problem2();
  2. 关闭问题2();
  3. instead of document.write(); u can specify a id like

    而不是document . write();可以指定一个id。

    or

and in the js use

在js中使用。

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

for better and safer coding use jsLint ..

为了更好更安全的编码使用jsLint ..

#4


0  

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和Firefox找到很多有用的扩展,它们可以满足你的调试。

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

Chrome有一些内部工具,比如“网络控制台”,可以让你看一些变量。

#1


2  

You are missing a closing } after problem2.

问题2之后,你错过了一个结尾。

That is the most common 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.

这是造成这种错误的最常见原因。由于函数体未正确关闭,JS解释器会中断,停止解析。然后,其他一些东西试图引用一个变量/函数,该变量/函数未能被解析并触发引用错误。

#2


1  

I can see a few issues.

我能看到一些问题。

  1. You are missing a closing } after problem2.
  2. 问题2之后,你错过了一个结尾。
  3. You have spaces around your = for onclick =.
  4. 在= for onclick =中有空格。
  5. You're not actually doing anything with myArray. That does not seem intentional.
  6. 实际上你并没有使用myArray做任何事情。这似乎不是故意的。
  7. 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 {.
  8. 您的代码格式不清楚,这可能会导致以后的困难(我认为这部分是导致bug的原因)。除非你有一个特别好的理由,你应该避免像{这样的事情。改为在{。

#3


0  

  1. close the brace of problem2();
  2. 关闭问题2();
  3. instead of document.write(); u can specify a id like

    而不是document . write();可以指定一个id。

    or

and in the js use

在js中使用。

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

for better and safer coding use jsLint ..

为了更好更安全的编码使用jsLint ..

#4


0  

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和Firefox找到很多有用的扩展,它们可以满足你的调试。

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

Chrome有一些内部工具,比如“网络控制台”,可以让你看一些变量。