Skip to main content

Posts

Showing posts with the label for loop

JavaScript Basic: Study Note For Array Iteration

Array iteration refers to iterating over a list or an array. Basically, it loops through that array and do something to each item or with each item. Use For Loop We use for loop to loop over an array. In order to do that, we need to make use of the array's length property. For example, if we want to print out each item from an array, we need to recall the index of each item. In this case, we want to print out each letter from the array, so we have to type the code for times. It is repeating a lot. Image we have hundred or thousands data and print it all out, it will drive us crazy. Therefore, we use a loop to help automate this process because what we are doing here is the same operation. Using for loop can save our time and make the code clean.  var letters = ["a", "b", "c", "d"] for(var i = 0; i < letters.length; i++) { console.log(color[i]); } In the for loop, we have our variable i start with 0 because that ...

JavaScript Basic: For Loop Study Note

For loop is another way of repeating code. for( initial; condition; step) { //run some code } As you see, for loop includes three part: The first part is the initialize where we declare a variable and set it to some initial value. The second part is condition which is when this loop should keep running. The last part is step , so what do we do at the end of every iteration. I would like to share two examples from the course: Print numbers from 0 to 5 with a for loop I write the for loop in the console. There are three part in the for loop:  1. Initial: count start from 0 (count = 0)  2. Condition: run the code until the count is less than 6 ( count < 6) 3. Step: count incrementally The result list 0 to 5. Print each character in a string with a for loop 1. Initial: list the character from the beginning (start 0)  2. Condition: run the code until it end by the string length  3. Step: list all the characters ...