0

Why method push() doesn't work?

Why method push() doesn't work in this code: https://code.sololearn.com/W7NJh3k60dk7/?ref=app

17th Mar 2019, 12:40 PM
AsediX
5 Réponses
+ 2
The push statements should only be executed if e.g. world[y-1] != undefined. But if e.g. y=0 then it will try to access world[-1], which doesn't exist so it will never create var up. I commented out the error catching and moved the push statements inside the world[y-1] conditionals. No more errors and if e.g. y=0 it simply won't try to push. https://code.sololearn.com/Wq5rPYbd4f5E/?ref=app
17th Mar 2019, 7:34 PM
Jassi Rooste
Jassi Rooste - avatar
+ 1
In what line is the problem?
17th Mar 2019, 7:17 PM
Juan David Padilla Diaz
Juan David Padilla Diaz - avatar
+ 1
I viewed the link on my laptop and it gave me warning icons/info for lines (heads up as the mobile view doesn’t do this) It’s telling me on line 48 that ‘up’ used out of scope. I would try declaring the variables you are setting in the preceding if statements before the try catch block e.g var get_true = []; var up; if(...) up = ... ; Then use it in the try catch block. My guess is that those variables are only in the scope of the if statement and not the rest of your function so any array.push() calls would be pushing undefined variables
17th Mar 2019, 7:21 PM
Jenine
+ 1
Remember you're doing that inside a function, also it didn't have any problem with your code on phone, either way I'll run it on my PC and see what's going on
17th Mar 2019, 7:25 PM
Juan David Padilla Diaz
Juan David Padilla Diaz - avatar
+ 1
Sorry, I fixed this error a little earlier. But thanks for trying to help :)
17th Mar 2019, 8:03 PM
AsediX