Why is the first for loop running only once? When I comment out the second for-loop that is within, the first for-loop loops through and does not loop just once.   function a(n){           var count =0;           for (var i=0;i&lt;n;i++){       count+=1;       console.log('loop 1, counter: ' + count)                   for (var i=0;i&lt;5*n;i++){                       count+=1;        console.log('loop 1^2, counter: ' + count)                    }           }   return count;      }                                                                                 console.log(a(5)
122
What is the scope of variables in javascript? Do they have the same scope inside as opposed to outside a function? Or does it even matter? Also, where are the variables stored if they are defined globally?
111161
Am I on the right track? I am not sure about my reasoning... Number of surjective functions from $A$ to $B$ $$A = \{1,2,3,4\} ; B = \{a,b,c\}$$ We must count the surjective functions, meaning the functions for which for all $b \in B$, $\exists~a \in A$ such that $f(a) = b$, $f$ being one of those functions. In order for a function $f:A\rightarrow B$ to be a surjective function, all 3 elements of $B$ must be mapped. We need to count how many ways we can map those 3 elements. We will subtract the number of functions from $A$ to $B$ which only maps 1 or 2 elements of $B$ to the number of functions from $A$ to $B$ (computed in 4.c : 81). Only 1 element of $B$ is mapped The first $a \in A$ has three choices of $b \in B$. The others will then only have one. Total functions from $A$ to $B$ mapping to only one element of $B$ : 3. Exactly 2 elements of $B$ are mapped Similarly, there are $2^4$ functions from $A$ to $B$ mapping to 2 or less $b \in B$. However, these functions include the ones that map to only 1 element of $B$. So there are $2^4-3 = 13$ functions respecting the property we are looking for. In the end, there are $(3^4) - 13 - 3 = 65$ surjective functions from $A$ to $B$.
74382
i have:  for i in range(2,n):     if(something):        do something     else:        do something else        i = 2 **restart the loop   But that doesn't seem to work. Is there a way to restart that loop?   Thanks
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?
93651
I want to compare 2 arrays of objects in JavaScript code.  The objects have 8 total properties, but each object will not have a value for each, and the arrays are never going to be any larger than 8 items each, so maybe the brute force method of traversing each and then looking at the values of the 8 properties is the easiest way to do what I want to do, but before implementing, I wanted to see if anyone had a more elegant solution. Any thoughts?
35871
How does Python evaluate the expression 1+++2?  How many ever + I put in between, it is printing 3 as the answer. Please can anyone explain this behavior  And for 1--2 it is printing 3 and for 1---2 it is printing -1
15584
In C, what is the difference between using ++i and i++, and which should be used in the incrementation block of a for loop?
82094
How can I build a loop in JavaScript?
22243
I am having difficulty getting a for loop to take user input in the form of an argument to a function I created. #!/bin/bash number1=&quot;$1&quot;    #Assume user input 5 loops=&quot;$2&quot;      #Assume user input 5 if [[ ${number1} &amp;&amp; ${loops} =~ ^-?[0.00-9.99]+$ ]];then    for number2 in {1..${loops}}    do        Product=$(echo &quot;${number1} * ${number2}&quot; | bc -l)        echo &quot;${number1} * ${number2} = ${Product}&quot;        if test ${number2} -eq 7;then            { echo &quot;----------------------&quot;; echo &quot;${number2} is the best number.&quot;; echo &quot;----------------------&quot;; continue; }        elif test ${number2} -eq 11;then            { echo &quot;------------------------------&quot;; echo &quot;${number2} is pretty awesome as well.&quot;; echo &quot;------------------------------&quot;; continue; }            fi     done else    { echo &quot;Please try again with a valid numerical entry.&quot;; exit 1; } fi  This script does not run when the user inputs an integer for the variable &quot;loops&quot;, and any number for &quot;number1&quot;. number1 = 5 in this case. It returns the error:  (standard_in) 1: syntax error 5 * {1..5} = /home/$USER/For_Loop_Multiplication.sh: line 9: test: {1..5}: integer expression expected /home/$USER/For_Loop_Multiplication.sh: line 11: test: {1..5}: integer expression expected  If I instead directly input an end bound on the number of loops directly into the script like the following, and number1 = 5, it runs fine: #!/bin/bash number1=&quot;$1&quot;     #Assume user input 5 if [[ ${number1} =~ ^-?[0.00-9.99]+$ ]];then    for number2 in {1..5}    do        Product=$(echo &quot;${number1} * ${number2}&quot; | bc -l)        echo &quot;${number1} * ${number2} = ${Product}&quot;        if test ${number2} -eq 7;then            { echo &quot;----------------------&quot;; echo &quot;${number2} is the best number.&quot;; echo &quot;----------------------&quot;; continue; }        elif test ${number2} -eq 11;then            { echo &quot;------------------------------&quot;; echo &quot;${number2} is pretty awesome as well.&quot;; echo &quot;------------------------------&quot;; continue; }            fi     done else    { echo &quot;Please try again with a valid numerical entry.&quot;; exit 1; } fi  It returns the result:  5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25  Essentially what I think is happening is that the script seems to be placing the entire range as the value number2, which is defined as the iterator in the for loop. Since the input for the variable &quot;loops&quot; is an integer, there should be no difference between asking the user for a range and placing it in the loop, or simply setting it as a default when I write the script. However, there apparently is a difference... Any insight into solving this problem is appreciated. Thanks.
110764
In the past and with most my current projects I tend to use a for loop like this:  var elements = document.getElementsByTagName('div'); for (var i=0; i&lt;elements.length; i++) {     doSomething(elements[i]); }   I've heard that using a "reverse while" loop is quicker but I have no real way to confirm this:  var elements = document.getElementsByTagName('div'),      length = elements.length;  while(length--) {     doSomething(elements[length]); }   What is considered as best practice when it comes to looping though elements in JavaScript, or any array for that matter?
3385
In the two following snippets, is the first one safe or must you do the second one?  By safe I mean is each thread guaranteed to call the method on the Foo from the same loop iteration in which the thread was created?  Or must you copy the reference to a new variable "local" to each iteration of the loop?  var threads = new List&lt;Thread&gt;(); foreach (Foo f in ListOfFoo) {           Thread thread = new Thread(() =&gt; f.DoSomething());     threads.Add(thread);     thread.Start(); }   -  var threads = new List&lt;Thread&gt;(); foreach (Foo f in ListOfFoo) {           Foo f2 = f;     Thread thread = new Thread(() =&gt; f2.DoSomething());     threads.Add(thread);     thread.Start(); }   Update: As pointed out in Jon Skeet's answer, this doesn't have anything specifically to do with threading.
24071
I'm trying to understand Python's approach to variable scope. In this example, why is f() able to alter the value of x, as perceived within main(), but not the value of n?  def f(n, x):     n = 2     x.append(4)     print('In f():', n, x)  def main():     n = 1     x = [0,1,2,3]     print('Before:', n, x)     f(n, x)     print('After: ', n, x)  main()   Output:  Before: 1 [0, 1, 2, 3] In f(): 2 [0, 1, 2, 3, 4] After:  1 [0, 1, 2, 3, 4]
12135
I want to iterate through a dropdown, select the first value, then click search, then again select the second value and again perform search.In this code i have tried selecting only one option,but I want to perform search on all the options one by one
51995
I was reading this thread:   What is IFS? And what is its usage in context of for-loops?
30464
I have the following bash script:  #!/bin/bash  upperlim=10  for i in {0..10} do echo $i done  for i in {0..$upperlim} do echo $i done   The first for loop (without the variable upperlim in the loop control) works fine, but the second for loop (with the variable upperlim in the loop control) does not.  Is there any way that I can modify the second for loop so that it works?  Thanks for your time.
4181
What's the best way to break from nested loops in Javascript?  //Write the links to the page. for (var x = 0; x &lt; Args.length; x++) {    for (var Heading in Navigation.Headings)    {       for (var Item in Navigation.Headings[Heading])       {          if (Args[x] == Navigation.Headings[Heading][Item].Name)          {             document.write("&lt;a href=\""                 + Navigation.Headings[Heading][Item].URL + "\"&gt;"                 + Navigation.Headings[Heading][Item].Name + "&lt;/a&gt; : ");             break; // &lt;---HERE, I need to break out of two loops.          }       }    } }
62595
i am writing script which uses different Tools of arcgis and implementing different functionalities .. My script consists in different Functions running in an order. Each function is connected to each other , and works on layers and performs   functions like(update cursor, search cursor ) When function1 finishes execution funciton2 starts and works on the same layer on which first one was working.   The problem is here function2 generates wrong results or works wrong.  works wrong means if a loop is defined to 5 iterations it will runs more then 5 times. like 20, 30 times and generates different results of one value.  When i run each function separately it will works fine. but when i run function in one script it will create problems.  My findings of this are LOCKS which are creating the problem.  Sample Structure of my script:  import arcpy   import math   import sys     import traceback     from arcpy import env    class test:       def __init__(self, workSpace):          self.workSpace = workSpace        # Add Fields       def function1(self):             # Script               return;           # Generate Report       def function2(self):              # Script            rows = arcpy.SearchCursor(self.mapLayerFinal,"","","Shape_Area")           row = rows.next()           while row:                   area = row.Shape_Area                     print "function2"                         destination_cursor = arcpy.UpdateCursor(self.censusLayer, '"DISTRICT_ID" = '+ str(district_id))                   row1 = destination_cursor.next()                   while row1:                           row1.setValue("District_Area",area)                           destination_cursor.updateRow(row1)                            row1 = destination_cursor.next()                    row = rows.next()             return;        t = test(workSpace)   t.function1()   t2 = test(workSpace)   t2.function2()     NOTE: code is tested and working correct if each function is executed separately