-
Notifications
You must be signed in to change notification settings - Fork 30
Loops
Loops are everywhere in programming. It is the one thing that allows you to do infinite repetitions. So take some time to understand these constructs. They will be the base of most of your scripts
while ( Boolean ) { /* Do Stuff Here! */ }
for ( var i = 0; i < thing.length; i++ ){ /* Do Stuff Here! */ }
for (var key in Object ){ /* Do Stuff Here! */ }
var x = [1,2,3]
for ( var i = 0; i < x.length; i++ ) {
x[i] += 1;
}
alert(x);
Let's play with loops
/*
Loops
*/
// this is our string
var source_string = "Hi there. I am a string please chop me into pieces";
var source_array = new Array(); // create a new array like this
var target_array = []; // or like this
var target_string; // this will hold our result
var delimiter = " "; // this is what we use to split and join our string
var source_array = source_string.split (delimiter);// split the string into an array
alert("Source string: "+source_string + "\nTarget array:\n"+ source_array); // lets see what it did
/*
Now loop that array we created
there are three parts in this for loop construct
var index = 0; // this is our counter
than follows a condition
index < source_array.length
as long as this is true we do the third part
index = index + 1; // we increment the index by one
Than the condition loop block { } is executed
when the block is done he will again check if the condition
index < source_array.length
is still true. If so increment by one and execute the block { }
you can stop a loop by using break;
like:
if(index == 5){
break;
}
or step over one index by using
if(index == 5){
continue
}
Play with it a lot. Don't worry you wont go blind
*/
for(var index = 0; index < source_array.length; index = index + 1 ){
target_array.push(source_array[index]);
}
// lets see what happened in the target array
alert("Target array: "+target_array);
// join the array again
target_string = target_array.join (delimiter);
// hey we got our string back
alert("hey we got our string back:\n"+ target_string);
There is a different kind of loop. The while
loop. He does the same as every other loop but depends more on a condition than in/decreasing an index. Have a look at it:
var highest_value = 20;
var counter = 0;
var aNumber = 200;
while(counter < highest_value){
aNumber-=20;
counter++;
}
alert("Our result is: "+ aNumber);
The third kind of loop the for(var key in object)
can be used to loop objects. Here the key is not can be an index. Important is it also can be the name of a property. Don't use these loops an Arrays! Have a look at this stackoverflow
var obj = {
"a":"Hello World",
"b":5.5,
"c":true,
"x":[1,2,3]
};//Object
for ( var key in obj ){
if (obj.hasOwnProperty(key)){
alert( key );
}
}
alert(obj["a"]);
You can also create for loops like the following one. But you wont see this very often.
var i =0;
var counter = 5
for(i;i < 5;i++,counter++){
alert(counter);
}
This wiki is maintained by:
fabiantheblind
Thanks to:
- JohnDarnell for fixing lots of typos.
- jsp for fixing lots of typos.
- ltfschoen for fixing typos.
- wridgers for adding more links.
Thanks to the students from the seminar for asking all those questions and making me start this wiki.
- adinaradke
- AnitaMei
- ce0311
- coerv
- felixharle
- FerdinandP
- Flave
- marche
- monkian
- natael
- OliverMatelowski
- PDXIII
- praktischend
- schlompf
- skaim
You are awesome.
- Arrays
- Classes
- Comments
- Conditionals
- Functions
- Inspect Properties
- Loops
- Objects
- Output And Interaction
- Recursive Functions
- Inspect Properties
- Variables And Operations
- Extended JavaScript Guide
- Bridge Talk
- Create And Read Files
- ExtendScript Toolkit
- File
- Folder
- Includes JSX
- Object Watch
- Read In JSON From File And DONT Eval
- Storing Data In A Target Engine
- Target an application
- XML
- app
- Colorbrewer
- Colors And Swatches
- Delay And View
- Dialogs
- Documents
- Duplicate And Transform
- Event AfterSave
- Export IDML
- ExtendScript in InDesign Scripting DOM
- Fonts
- GeometricBounds and Coordinates
- Get named pageItems
- Graphic Lines
- Groups
- HSL Color Wheel
- Images
- Includes
- InsertionPoints
- Layers
- Line Feeds And Carrige Returns
- Masterspreads
- Matrix
- Objectstyles
- Outlines Groups Alignment
- Pages And Margins
- Pathfinder
- Placeholder Text
- Rectangles Ovals Polygons
- RulerOrigin
- Select words at insertionPoint
- Simple Find And Change Grep with FC Query
- Simple Find And Change Grep
- Simple Find And Change Text
- Spiro
- Styles
- Text Analysis ID FC
- Text Analysis
- Text Find Locations
- Text
- Transformation Matricies
- TransparencySettings
- XML creation and import