WARNING: Probably not a 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. I just want to know why that is.
The problem is with your y
variable. In your check y < queries.length
you first check for the length and then increment.
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. The last element of queries
is queries[2]
, though. So queries[3]
is undefined
and inside the function, you try query[0]
which is accessing property 0
of undefined
. 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:
if ( y < queries.length - 1 ){
Also, a bit of an optimization. You don't need the y
variable. It's basically just x
but passed along as an argument. You have access to x
from recurring()
so you might as well use it :
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. At one point, y
will equal 2
.
It will be incremented and will equal 3
. queries[3]
doesn't exist and will be undefined
. (because, you do know that an array's first index is 0, do you?)
Then, in the next recurring
, you'll call query[0]
on it, but it is undefined
: it will crash.
That's all ;) You if
condition is not good ;)
It must be if ( y < queries.length - 1 )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.