I'm trying to figure out why this for loop does not render out 0,1,2,3 like its suppose to.  It renders 4, 4 , 4, 4.  How would i get this to print 0, 1, 2, 3 with the setTimeout still in place ?  for (var i = 0; i &lt; 4; i++) {        setTimeout(function() {              console.log('The index of this number is: ' + i);        }, 3000);  }
1086
How would you explain JavaScript closures to someone with a knowledge of the concepts they consist of (for example functions, variables and the like), but does not understand closures themselves?  I have seen  given on Wikipedia, but unfortunately it did not help.
90954
Straightforward question, so if it is applied to every element of a vector that means that every one of them is scaled down exactly length times. How did people come up with this, to make it exactly 1? I am sorry if this is fairly obvious, but I just don't see it.
31696
I need to bitwise shift a value 64 times in JavaScript. But JavaScript starts rounding after 32.  For example:  for(var j = 0; j &lt; 64; j++) {     mask = mask &lt;&lt; 1;     console.log(mask); }   This prints value from 0 to 1073741824 but then rounds of and starts printing 0.
82090
int i = 4; string text = "Player "; cout &lt;&lt; (text + i);   I'd like it to print Player 4.  The above is obviously wrong but it shows what I'm trying to do here. Is there an easy way to do this or do I have to start adding new includes?
65652
In bash, I know that it is possible to write a for loop in which some loop control variable i iterates over specified integers.  For example, I can write a bash shell script that prints the integers between 1 and 10: #!/bin/bash  for i in {1..10} do  echo $i done  Is it possible to instead iterate over a loop control variable that is a string, if I provide a list of strings?  For example, suppose that I have a string fname that represents a file name.  I want to call a set of commands for each file name.  For example, I might want to print the contents of fname using a command like this: #!/bin/bash  for fname in {&quot;a.txt&quot;, &quot;b.txt&quot;, &quot;c.txt&quot;} do  echo $fname done  In other words, on the first iteration, fname should have the value fname=&quot;a.txt&quot;, while on the second iteration, fname should have the value fname=&quot;b.txt&quot;, and so on.  Unfortunately, it seems that the above syntax is not quite correct.  I would like to obtain the output:  a.txt b.txt c.txt  but when I try the above code, I obtain this output:  {a.txt, b.txt, c.txt}  Can you please help me determine the correct syntax, so that I can iteratively change the value/contents of the variable fname?  Thank you for your time.
27582
I have a set of global counter variables in Javascript:  var counter_0 = 0; var counter_1 = 0; var counter_2 = 0;   etc  I then have a Javascript function that accepts an 'index' number that maps to those global counters. Inside this function, I need to read and write to those global counters using the 'index' value passed to the function.  Example of how I'd like it to work, but of course doesn't work at all:  function process(index) {     // do some processing      // if 'index' == 0, then this would be incrementing the counter_0 global variable     ++counter_+index;       if (counter_+index == 13)     {         // do other stuff     } }   I hope what I'm trying to accomplish is clear. If not I'll try to clarify. Thanks.  EDIT Clarification:  I'm not trying to increment the name of the counter, but rather the value the counter contains.
3799
How do I access the index in a for loop like the following?  ints = [8, 23, 45, 12, 78] for i in ints:     print('item #{} = {}'.format(???, i))   I want to get this output:  item #1 = 8 item #2 = 23 item #3 = 45 item #4 = 12 item #5 = 78   When I loop through it using a for loop, how do I access the loop index, from 1 to 5 in this case?
87194
I've been told that code such as:  for (int i = 0; i &lt; x.length(); i++) {     // blah }   is actually O(n^2) because of the repeated calls to x.length(). Instead I should use:  int l = x.length(); for (int i = 0; i &lt; l; i++) {     // blah }   Is this true? Is string length stored as a private integer attribute of the String class? Or does String.length() really walk the whole string just to determine its length?
4327
Is there a way in Java's for-each loop  for(String s : stringArray) {   doSomethingWith(s); }   to find out how often the loop has already been processed?  Aside from using the old and well-known for(int i=0; i &lt; boundary; i++) - loop, is the construct  int i = 0; for(String s : stringArray) {   doSomethingWith(s);   i++; }   the only way to have such a counter available in a for-each loop?
48480
On my local machine, where I run CentOS 7.2, when I do this:  for i in {8..12}; do echo ${i}; done   I got:  8 9 10   But when I do this:  for i in {08..10}; do echo ${i}; done   I got:  08 09 10   But on server to which I have access, which runs CentOS 5.6, running both commands results in the same output:  8 9 10   Why there are no zeroes in front of 8 and 9? Is it because of bash version, or just environment configuration? Can I change it per session or permanently, so my server will put that zero in front of numbers smaller than 10?
123088
I am writing a SQL query creator using some parameters. In Java, it's very easy to detect the last element of an array from inside the for loop by just checking the current array position with the array length.  for(int i=0; i&lt; arr.length;i++){      boolean isLastElem = i== (arr.length -1) ? true : false;         }   In PHP they have non-integer indexes to access arrays. So you must iterate over an array using a foreach loop. This becomes problematic when you need to take some decision (in my case to append or/and parameter while building query).   I am sure there must be some standard way of doing this.   How do you solve this in PHP?
17717
I wish to print a Stack&lt;Integer&gt; object as nicely as the Eclipse debugger does (i.e. [1,2,3...]) but printing it with out = "output:" + stack doesn't return this nice result.  Just to clarify, I'm talking about Java's built-in collection so I can't override its toString().  How can I get a nice printable version of the stack?
109583
I want to render many(about 1000) components and I am displaying them through aura:iteration in another aura:iteration. This is causing me performance issues. I want to display only 10 records at an instance, and on scroll of mouse, load next 10 and so on...It should be like the way feed is loaded on a FB page or chatter. Any ideas how can this be implemented?     markup:   &lt;aura:iteration var="outerListElement" items="{!v.listOfLists}"&gt;    &lt;tr&gt;       &lt;aura:iteration var="singleEntry" items="{outerList"&gt;        &lt;td &gt;            &lt;div class="slds-truncate"&gt;                &lt;c:newComponent singleEntry="{!singleEntry}" outerListElement="{!outerListElement}" attr1="{!v.attr1OfParent}"&gt;            &lt;/div&gt;         &lt;/td&gt;       &lt;/aura:iteration&gt;    &lt;/tr&gt; &lt;/aura:iteration&gt;
21175
Is there a way to determine if the loop is iterating for the last time. My code looks something like this:  int[] array = {1, 2, 3...}; StringBuilder builder = new StringBuilder();  for(int i : array) {     builder.append("" + i);     if(!lastiteration)         builder.append(","); }   Now the thing is I don't want to append the comma in the last iteration. Now is there a way to determine if it is the last iteration or am I stuck with the for loop or using an external counter to keep track.
26339
Is there some rare language construct I haven't encountered (like the few I've learned recently, some on Stack Overflow) in C# to get a value representing the current iteration of a foreach loop?  For instance, I currently do something like this depending on the circumstances:  int i = 0; foreach (Object o in collection) {     // ...     i++; }
17043
I have a Python script which takes as input a list of integers, which I need to work with four integers at a time.  Unfortunately, I don't have control of the input, or I'd have it passed in as a list of four-element tuples.  Currently, I'm iterating over it this way: for i in range(0, len(ints), 4):     # dummy op for example code     foo += ints[i] * ints[i + 1] + ints[i + 2] * ints[i + 3]  It looks a lot like &quot;C-think&quot;, though, which makes me suspect there's a more pythonic way of dealing with this situation.  The list is discarded after iterating, so it needn't be preserved.  Perhaps something like this would be better? while ints:     foo += ints[0] * ints[1] + ints[2] * ints[3]     ints[0:4] = []  Still doesn't quite &quot;feel&quot; right, though.  :-/ Related question:
66237
I have seen some very weird for loops when reading other people's code. I have been trying to search for a full syntax explanation for the for loop in C but it is very hard because the word "for" appears in unrelated sentences making the search almost impossible to Google effectively.  This question came to my mind after reading  which made me curious again.  The for here:   for(p=0;p+=(a&amp;1)*b,a!=1;a&gt;&gt;=1,b&lt;&lt;=1);   In the middle condition there is a comma separating the two pieces of code, what does this comma do? The comma on the right side I understand as it makes both a&gt;&gt;=1 and b&lt;&lt;=1.   But within a loop exit condition, what happens? Does it exit when p==0, when a==1 or when both happen?  It would be great if anyone could help me understand this and maybe point me in the direction of a full for loop syntax description.