PDA

View Full Version : Loops in JavaScript



doddz321
Mar 11th, 2010, 01:13 AM
Hello all.

I've spent a few days now browsing this forum along with guides on the net, I'm new to JS and I'm having a bit of trouble with loops. I just don't seem to get them, I was wondering if anyone had any tips or methods (the simpler the better :D) for getting these learnt and done with?

Would appreciate any tips

Thanks

jmrker
Mar 11th, 2010, 01:42 AM
Create and post a loop script that you believe should work.
We can comment on what you code does or does not do correctly. :thumbsup:

Otherwise, the best advise would be to work through on on-line tutorial about JS loops.
Lastly, as a noobie, do you have a beginners book on JS that covers loops?

doddz321
Mar 11th, 2010, 12:04 PM
I've looked at some online tutorials on W3Schools online and webdevelopernotes.com so I'm going through them.

I've also got a few javascript for beginners books that I've not delved too far into, I assume the loops will come to me with practise and by doing them. I don't know why I'm having such a hard time with them though

jmrker
Mar 11th, 2010, 01:38 PM
So ... post some code and we can discuss it, if you continue to have questions.

doddz321
Mar 11th, 2010, 03:06 PM
After reading through the online tutorials as you suggested, I think I've got it, so thank you for your help :)

jmrker
Mar 11th, 2010, 03:23 PM
You're most welcome. :thumbsup:
Happy to help, it's just my mind reading skills suck when it comes to questions to answer. :D
Good Luck!
:)

rnd me
Mar 12th, 2010, 12:08 AM
personally, i hardly ever use loops anymore. you might try avoiding them also.

Old Pedant
Mar 12th, 2010, 12:17 AM
personally, i hardly ever use loops anymore. you might try avoiding them also.

My head hurts.

How do you write code without using *some* loops???

Even such a simple thing as, say, showing one of many panels based on clicking on a tab?

I can imagine doing it without a loop, but it's *SO* much easier to do it with a loop.

And I sure don't see how you would write any reasonable JS game without loops.

jmrker
Mar 12th, 2010, 12:51 AM
personally, i hardly ever use loops anymore. you might try avoiding them also.

Loops are my friends. I use them whenever possible. :thumbsup:

What do you use in their place? Endless if..then..else statements? Switch..case statments?

Without loops I go loopy!

rnd me
Mar 12th, 2010, 02:53 AM
What do you use in their place? Endless if..then..else statements? Switch..case statments?


well, i also try to avoid switches, and i dislike trace-multiplying ifs in loops. obviously, sometime they can't be avoided.

my code use loops deep down, but I don't use them personally.

i do need the occasional while loop, but i seldom use incremental for iterations.

two major limitations of JavaScript's loops discourage me from using them:

1. no scope.
2. my processing logic gets stuck inside the loop


Loops are stuck in a single place, so i can't use the loop from anywhere.
for one-shot operations, this is no biggie.
for the kind of coding i often find myself working on, loops would have to be copied and pasted many times throughout my project.

essentially, loops means that if i need to fix a bug, i would now have several loops to fix.


Instead, i use arrays and [].map() instead of loops.
this lets me accomplish the same thing as a loop, but with the bonuses of scope, code-reuse, and centralization.

[].map isn't the only good one. One downside to [].map is that it can't break.
since loops don't return anyway, often times i don't need [].map's output array.
In that case, i can execute a break-able loop using [].some().
by returning false (or nothing) in the function that [].some() runs, it will keep going until a truthful value is returned, upon which it breaks.

i also enjoy the ability to [].filter() a set just before i [].map() it, something that would traditionally require touching loop code, and probably another if.

in sum, the various Array 1.6/7 (https://developer.mozilla.org/en/New_in_JavaScript_1.6#Array_extras), or ECMA5 (http://wiki.github.com/ry/node/ecma-5mozilla-features-implemented-in-v8)array methods provide me 90% coverage when it comes to looping/iteration.

i can't imagine working without them.


here is the support code i currently use: (<1kb)

//dandavis's ES5 Array methods:
(function ArrayMethods(){var o=Array.prototype,it,i,
e=eval('( {map:Z0,r=[];for(;i<m;i++){if(i in t){r[i]=a.call(b,t[i],i,t);}}return r;},filter:Z0,r=[],g=0;for(;i<m;i++){if(i in t&&a.call(b,t[i],i,t)){r[g++]=t[i];}}return r;},every:Z0;return m&&t.filter(a,b).length==m;},some:Z1;for(;m--;){if(m in t&&a.call(t,t[m],m,t)&&!--i){return true;}}return false;},lastIndexOf:Zb||-1;for(;m>i;m--){if(m in t&&t[m]===a){return l;}}return-1;},indexOf:Zb||0;for(;i<m;i++){if(i in t&&t[i]===a){return i;}}return-1;},reduce:Z0,r=b||t[i++];for(;i<m;i++){r=a.call(null,r,t[i],i,t);}return r;},reduceRight:Zm-1,r=b||t[i--];for(;i>-1;i--){r=a.call(null,r,t[i],i,t);}return r;},forEach:function(a,b){this.concat().map(a,b);return this;}})'.replace(/Z/g,"function(a,b){var t=this.concat(),m=t.length,i="));for(it in e){i=o[it];o[it]=i||e[it];}
}());//end ArrayMethods()

having these also cuts down on the need for lots of custom Array.prototype methods, so they reduce pollution.

Philip M
Mar 12th, 2010, 07:27 AM
here is the support code i currently use: (<1kb)

//dandavis's ES5 Array methods:
(function ArrayMethods(){var o=Array.prototype,it,i,
e=eval('( {map:Z0,r=[];for(;i<m;i++){if(i in t){r[i]=a.call(b,t[i],i,t);}}return r;},filter:Z0,r=[],g=0;for(;i<m;i++){if(i in t&&a.call(b,t[i],i,t)){r[g++]=t[i];}}return r;},every:Z0;return m&&t.filter(a,b).length==m;},some:Z1;for(;m--;){if(m in t&&a.call(t,t[m],m,t)&&!--i){return true;}}return false;},lastIndexOf:Zb||-1;for(;m>i;m--){if(m in t&&t[m]===a){return l;}}return-1;},indexOf:Zb||0;for(;i<m;i++){if(i in t&&t[i]===a){return i;}}return-1;},reduce:Z0,r=b||t[i++];for(;i<m;i++){r=a.call(null,r,t[i],i,t);}return r;},reduceRight:Zm-1,r=b||t[i--];for(;i>-1;i--){r=a.call(null,r,t[i],i,t);}return r;},forEach:function(a,b){this.concat().map(a,b);return this;}})'.replace(/Z/g,"function(a,b){var t=this.concat(),m=t.length,i="));for(it in e){i=o[it];o[it]=i||e[it];}
}());//end ArrayMethods()



But I thought you said that eval is evil ...... :D:D

rnd me
Mar 12th, 2010, 09:21 AM
But I thought you said that eval is evil ...... :D:D

i never said that; it's a very very good thing to me...

Philip M
Mar 12th, 2010, 09:35 AM
i never said that; it's a very very good thing to me...

http://www.codingforums.com/showthread.php?t=20143

rnd me
Mar 12th, 2010, 09:12 PM
But I thought you said that eval is evil ...... :D:D

no need for a debate about eval.
jeez phillip, im starting to feel like you have it in for me for sometimes; can i do anything right?


if you need to save the extra 1.5ms of unpacking time, use my uncompressed version (1.2kb):



(function(){var o=Array.prototype,it,i,e={
map:function(a,b){var t=this.concat(),m=t.length,i=0,r=[];for(;i<m;i++){if(i in t)r[i]=a.call(b,t[i],i,t)}return r},
filter:function(a,b){var t=this.concat(),m=t.length,i=0,r=[],g=0;for(;i<m;i++){if(i in t&&a.call(b,t[i],i,t)){r[g++]=t[i]}};return r},
every:function(a,b){var t=this.concat(),m=t.length,i=0;return m&&t.filter(a,b).length==m},
some:function(a,b){var t=this.concat(),m=t.length,i=1;for(;m--;){if(m in t&&a.call(t,t[m],m,t)&&!--i){return!0}}return!1},
lastIndexOf:function(a,b){var t=this.concat(),m=t.length,i=b||-1;for(;m>i;m--){if(m in t&&t[m]===a){return l}}return-1},
indexOf:function(a,b){var t=this.concat(),m=t.length,i=b||0;for(;i<m;i++){if(i in t&&t[i]===a){return i}}return-1},
reduce:function(a,b){var t=this.concat(),m=t.length,i=0,r=b||t[i++];for(;i<m;i++){r=a.call(null,r,t[i],i,t)}return r},
reduceRight:function(a,b){var t=this.concat(),m=t.length,i=m-1,r=b||t[i--];for(;i>-1;i--){r=a.call(null,r,t[i],i,t)}return r},
forEach:function(a,b){this.concat().map(a,b)}
};for(it in e){i=o[it];o[it]=i||e[it]}}()); //end Array.16 injection

Philip M
Mar 13th, 2010, 07:39 AM
jeez phillip, im starting to feel like you have it in for me for sometimes; can i do anything right?
[/CODE]

No, of course I don't have it in for you. My point is, as I have made in the past, that your lofty contribution is quite inappropriate to the level of experience of the OP, in this case a raw beginner who is having trouble understanding the rather fundamental concept of loops. As far as he is concerned, it might as well be written in Klingon. And, to be frank, I do not understand what the point is either. Like jmrker, loops are my friends and I use them whenever possible.

Old Pedant
Mar 14th, 2010, 06:43 AM
Well, I think rndme is only trying to say "bury your loops in library code" and then use the library functions instead of explicit loops.

But as you can see from his library, most every function there really *is* just a loop.

I would only say that not every problem that uses loops is also using an array. I point to my "show one tab from among several" example, earlier in the thread. Yes, you *could* use an array and then array methods, if you first placed the references to the tabs into an array. But that seems less natural to me than simply iterating through the sub-elements of a container <div>. But each to his/her own.

MrEnder
Mar 14th, 2010, 07:51 AM
omg you guys made my brain hurt

and

<3 philip

ok shoot me if I'm wrong but everywhere i read... eval = like satan in js... like .getElemenByIds what not are way better in most cases...