-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
for loop off by 1 #18
Comments
The
On a related note, it's a bad practice to use |
Are you sure? I am new to javascript (in fact I have yet to write a line of it!) so I may be mistaken, but from what I've just read, javascript's scope is either global or by function, not by block. I have seen several stackoverflow answers that say this, and I also tried your example above in jsfiddle and I can still access n outside of the for loop - even if I stick a "var" in front of the "n=0" . There is a new (since 2009?) "let" keyword that can do what you say, but it does not seem to me that the variable is destroyed unless you go out of your way to do so (with let keyword).
I am curious why you say that. What you said above ( for-loop scope ) is true in Lua, and that seemed like a wart to me. I agree it's bad to pollute the global namespace, but I don't see the harm in the way python keeps it around. Lastly,
I tried your fixed example above, and it still has n = 5 after the for loop is completed. Off topic, just wanted to say - I am very excited about Rapydscript - I have put off learning Javascript for nearly 20 years, turned off by the C-like syntax. Rapydscript "feels" right, without the bloat of the other python-in-browser projects - Thank you for writing it! |
Yes, if the variable is properly declared inside the loop (as below), it will be destroyed:
If you forget
I just tested the example, and you're right that it doesn't fix it, I guess I was wrong about order of operations in the conditional check, I'll fix it by decrementing it at the end of the loop instead. I'm glad you like RapydScript, that's exactly the same motivation I had for writing it, after playing with some of the more bloated alternatives. I encourage you to join our mailing list as well: https://groups.google.com/forum/?fromgroups#!forum/rapydscript |
I did try this, see: http://jsfiddle.net/j22d4/ and n is still alive (not destroyed), so I am not sure what you mean...? I will check out the mailing list! Thanks |
You're right, I was testing it incorrectly. |
Hmm, rather than fixing this I think I'll have the linter prevent this usage. Using loop-specific variables outside of a loop is poor design - and in fact not possible in many other languages including perl (and soon won't be possible in native JavaScript with the addition of |
With this code:
I get this output:
Rapydscript generates this code:
It does not matter if I "include stdlib" or not.
But, everything works as expected if I do this:
In this version, Rapydscript uses the stdlib range() function
The text was updated successfully, but these errors were encountered: