# Iteration with Javascript

Iteration is ideal work for a computer and it's quite worthwhile for folks embarking on the study of chaos to see some basic computer code. A natural way to perform iteration in Javascript (and many other computer languages) is with a loop. If you just want to iterate a certain number of times, a for loop is probably the easiest way to go. I guess the code should look like this:

for(i=0; i<bail; i++) {
'do something'
}


If bail is set, that little snippet should execute 'do something' that many times. Of course, 'do something' should do something, rather than nothing. In our case, we'd like to apply a function to a previously defined value and push the result onto a list. The whole thing might look like so:

x = x0;                     // Starting point
list = [x0];                // Initialize the list
for(i=0; i<bail; i++) {
x = f(x);                 // Compute the next value
list.push(x);             // push the next value onto list
}
list                        // return the result


If x0, bail, and f are defined, that code should run perfectly and generate the result of iterating f a total of bail times starting from x0.

You can try this in the little textarea below which is set up to accept code written in Javascript. Any code whose final result is an array of numbers should do. It could literally be as simple as [1,2,3] but our objective here is to study iteration. For those who don't know Javascript, the examples menu provides a start illustrating the ideas above. Those examples can be edited however you like. For those who do know Javascript, note that all the symbols from the Math namespace have been imported into the global namespace. This allows you to abbreviate a function like Math.cos(Math.PI*x) as just cos(PI*x). After you have an example, hit the generate button to automagically generate a timeseries plot and a table of the orbit.

Once you feel like you understand the examples, you can edit them just a little bit to change the function, starting point, or number of iterates. At some point you should try out your personal logistic function.