Monday, March 28, 2011

Week 9- Best Tutorial Recommendation- For Loops in Photoshop Javascript

This website: http://babbage.cs.qc.edu/courses/cs081/Javascript_Tutorial/index.xhtml#navTarget_2
has the fundamental breakdown on Javascript concepts.

The example in this tutorial discusses that for loops can be employed to use arrays in various ways. Addition of simple variable numbers in an array is the utilization for this example. First, the selected numbers have to be chosen and put into an array, which is not shown in the example. The variable sum will hold the added amount of all the variables from within the array. Sum has been declared at the top of the for loop to be recognized and used later on. The variable i within the for loop is a generic variable used as a counter also called a loop control variable. The variable i is used three times in the first line of the for loop. The value is always set to zero because that is the number programmers always use to and signifies the beginning of array elements and for loops . An ending has to be specified as well. This for loop can run the number of times stated in the array defined, which is four. The method call anArray.length measures the amount of elements in the for loop, which becomes the amount of times the for loops functions.  Lastly, i++ is the code that specifies that i should add one to every element in the array and add the element values, as demonstrated by the linking of  i to the array in the statement anArray[i].

This is the full code I came up with after I changed the numbers in the example and reviewed the loops and arrays section also provided on the website. I set up four variables for numbers in the array and the array itself. I made the mistake the first time of not properly declaring the type var for the loop and the array and the new type for the array as well. The types of everything have to be defined in a similar manner to regular Java programming, which I learned from my Java class last semester and made me familiar with for loops and their functions.

//For loop array example with a result of 135.
Every element value in the array is added to the
one before to come out with the result of 135.

var a= 20; //element 0, #1
var b= 35;// element 1, #2
var c= 55; //element 2, #3
var d = 25; //element 3, #4

var anArray = new Array (a, b, c, d);
var sum = 0;
for (var i = 0; i < anArray.length; i++)
{
           sum = sum + anArray[i];
}

No comments:

Post a Comment