+ 2
Baffling JavaScript behaviour
This is my code: https://code.sololearn.com/WEE9mZOZctgh/?ref=app Lines 53 and 56 have identical alerts, separated by an if statement. The first returns what I'd expect, but the second returns undefined. Why is this? Does passing the siblings nodeList to another function delete it? To create the error: enter any number in select box, then check and uncheck any checkbox. I've been stuck on this for hours so any answers would be gratefully received. Thanks.
15 ответов
+ 2
Russ your problem is very subtile... in loops you dont declare the x var therefore it will be global and every time that you refer to it you refer same x global var... Now inside clickFunc you use an x variable ( but not declare it when it will be global) and next you call a function that use same x var and set his value at some other value when returned to you original function x will contain another value (because its setted from checkCount function and they refer to SAME global x var)... In practice: declare always all you variable with var though they are temporary
+ 2
Wow! I narrowed it down, but more debugging required later.
+ 2
I've narrowed it down to the checkCount() function
https://code.sololearn.com/WJcbB9c852bJ/?ref=app
If y = 1, it's ok. If it's arr.length it goes wrong.
That's my only lead so far!
+ 2
Wow, well done KrOW! I've only just managed to find the same issue, after quite a few hours.
I noticed the values of x were changing after calling the checkCount function! You got there just before me!
Here's the code with lots of alerts in! Lol!
https://code.sololearn.com/WJcbB9c852bJ/#js
Best answer to KrOw for sure! :-)
+ 1
Does this happen in a standard (i.e. not SoloLearn) environment Russ?
+ 1
@Xan Yes, it was happening on my laptop (on Django) too.
+ 1
How weird! This has had me going for ages.
+ 1
Apologies for the delay Russ, I've only just got back. I'm looking again now! This is an intriguing puzzle!
+ 1
👍👍👍 The important is that you have understanded which was the problem and why
+ 1
Note, I've just found this tool:
http://jshint.com/
It would have discovered your problem!
I might use this in future too!
I've run your code through this, and removed all warnings... it gives the following code:
https://code.sololearn.com/WJcbB9c852bJ/#js
It spotted a couple of missing semicolons, and spotted the var issue.
+ 1
@Xan @KrOW thank you very much to both of you. I will make a habit of declaring vars in loops from now on.
@Xan: thank you for putting so much time into trying to sort this - hopefully you'll get some use from the tool you discovered!
@ KrOW: thanks for the advice!
You both have been great. Your efforts are very much appreciated. Thanks again.
+ 1
@KrOW I'm trying to mark your answer as best, but the app crashes immediately on selecting it so I can't :/
As soon as there's an update, I will try again.
+ 1
Russ 👍👍👍👍
..
P.S. Dont worry about marking my answer
0
That's great. What do you think it is?
0
If it helps: I've noticed that putting 'alert(siblings.length)' before the error-prone alert returns the figure I would expect.