+ 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.

22nd Apr 2018, 4:44 PM
Russ
Russ - avatar
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
22nd Apr 2018, 11:32 PM
KrOW
KrOW - avatar
+ 2
Wow! I narrowed it down, but more debugging required later.
22nd Apr 2018, 7:54 PM
Emma
+ 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!
22nd Apr 2018, 7:57 PM
Emma
+ 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! :-)
22nd Apr 2018, 11:53 PM
Emma
+ 1
Does this happen in a standard (i.e. not SoloLearn) environment Russ?
22nd Apr 2018, 7:51 PM
Emma
+ 1
@Xan Yes, it was happening on my laptop (on Django) too.
22nd Apr 2018, 7:53 PM
Russ
Russ - avatar
+ 1
How weird! This has had me going for ages.
22nd Apr 2018, 8:00 PM
Russ
Russ - avatar
+ 1
Apologies for the delay Russ, I've only just got back. I'm looking again now! This is an intriguing puzzle!
22nd Apr 2018, 10:46 PM
Emma
+ 1
👍👍👍 The important is that you have understanded which was the problem and why
22nd Apr 2018, 11:56 PM
KrOW
KrOW - avatar
+ 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.
23rd Apr 2018, 12:29 AM
Emma
+ 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.
23rd Apr 2018, 4:00 AM
Russ
Russ - avatar
+ 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.
23rd Apr 2018, 4:04 AM
Russ
Russ - avatar
+ 1
Russ 👍👍👍👍 .. P.S. Dont worry about marking my answer
23rd Apr 2018, 7:04 AM
KrOW
KrOW - avatar
0
That's great. What do you think it is?
22nd Apr 2018, 7:55 PM
Russ
Russ - avatar
0
If it helps: I've noticed that putting 'alert(siblings.length)' before the error-prone alert returns the figure I would expect.
22nd Apr 2018, 8:13 PM
Russ
Russ - avatar