[英]Name this typeError? Probably scope error
WARNING: Probably not a typeError. 警告:可能不是typeError。 Doing scripting algorithms for fun.
做脚本算法很有趣。 Trying to learn something/sharpen problem solving => I commented out the undefined array.
尝试学习/解决问题的方法=>我注释掉了未定义的数组。 I can't tell why it's throwing an error.
我不知道为什么会引发错误。
function arrayManipulation(n, queries) { var array = new Array(n).fill(0) var x = 0 var recurring = (argmnt, query, y) => { //problem start here var start = query[0], end = query[1] //problem end somewhere else var newArg = argmnt.map((el, index) =>{ if(index+1 >= start && index+1 <= end){ return query[2] + el }else{ return el } }) console.log(newArg) if ( y < queries.length ){ y += 1 recurring(newArg, queries[y], y) }else{ return newArg } } var solution = recurring(array, queries[x], x) } arrayManipulation(5, [[1, 2, 100], [2, 5, 100], [3, 4, 100]])
Although the script runs fine and give me the desired output. 尽管脚本可以正常运行,并提供所需的输出。 It throws a stderr in node or typeerror in JS.
它在节点中引发stderr或在JS中引发typeerror。 I just want to know why that is.
我只想知道为什么。
The problem is with your y
variable. 问题出在你的
y
变量上。 In your check y < queries.length
you first check for the length and then increment. 在检查
y < queries.length
, 首先检查长度, 然后递增。
If your array has a length of 3
(as it does in your example), you will check if y
is less than 3
and if it's 2
, it passes the test, gets incremented to 3
and then you pass queries[3]
to the next recurring()
invocation. 如果数组的长度为
3
(如示例所示),则将检查y
是否小于3
,如果它小于2
,则通过测试,将其递增为3
,然后将queries[3]
传递给下一个recurring()
调用。 The last element of queries
is queries[2]
, though. 不过,
queries
的最后一个元素是queries[2]
。 So queries[3]
is undefined
and inside the function, you try query[0]
which is accessing property 0
of undefined
. 因此,
queries[3]
是undefined
并且在函数内部,您尝试使用query[0]
来访问undefined
属性0
。 That's the error. 那是错误。
function arrayManipulation(n, queries) { var array = new Array(n).fill(0) var x = 0 var recurring = (argmnt, query, y) => { //problem start here var start = query[0], end = query[1] //problem end somewhere else var newArg = argmnt.map((el, index) =>{ if(index+1 >= start && index+1 <= end){ return query[2] + el }else{ return el } }) console.log(newArg) y += 1 // now is here if ( y < queries.length ){ // was here recurring(newArg, queries[y], y) }else{ return newArg } } var solution = recurring(array, queries[x], x) } arrayManipulation(5, [[1, 2, 100], [2, 5, 100], [3, 4, 100]])
You have two choices: either increment first and then do the check (as in the snippet above), or check for queries.length - 1
, like this: 您有两种选择:要么先增加,然后再进行检查(如上面的代码段所示),要么检查
queries.length - 1
,如下所示:
if ( y < queries.length - 1 ){
Also, a bit of an optimization. 另外,还有一些优化。 You don't need the
y
variable. 您不需要
y
变量。 It's basically just x
but passed along as an argument. 它基本上只是
x
但作为参数传递。 You have access to x
from recurring()
so you might as well use it : 您可以从
recurring()
访问x
,因此您最好使用它 :
function arrayManipulation(n, queries) { var array = new Array(n).fill(0) var x = 0 var recurring = (argmnt, query) => { //problem start here var start = query[0], end = query[1] //problem end somewhere else var newArg = argmnt.map((el, index) => { if (index + 1 >= start && index + 1 <= end) { return query[2] + el } else { return el } }) console.log(newArg) x += 1 if (x < queries.length) { recurring(newArg, queries[x], x) } else { return newArg } } var solution = recurring(array, queries[x]) } arrayManipulation(5, [ [1, 2, 100], [2, 5, 100], [3, 4, 100] ])
Here is what I got from your script: 这是我从您的脚本中得到的:
{
"message": "Uncaught TypeError: Cannot read property '0' of undefined",
"filename": "https://stacksnippets.net/js",
"lineno": 20,
"colno": 22
}
This is normal because of this part: 由于这部分,这是正常的:
if ( y < queries.length ){
y += 1
recurring(newArg, queries[y], y)
You have 3
queries. 您有
3
查询。 At one point, y
will equal 2
. 在某一点上,
y
等于2
。
It will be incremented and will equal 3
. 它会增加并等于
3
。 queries[3]
doesn't exist and will be undefined
. queries[3]
不存在,将是undefined
。 (because, you do know that an array's first index is 0, do you?) (因为,您确实知道数组的第一个索引为0,对吗?)
Then, in the next recurring
, you'll call query[0]
on it, but it is undefined
: it will crash. 然后,在下一次
recurring
,您将对其调用query[0]
,但是它是undefined
:它将崩溃。
That's all ;) You if
condition is not good ;) 仅此而已;)
if
条件不好,您if
可以了;)
It must be if ( y < queries.length - 1 )
它必须是
if ( y < queries.length - 1 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.