JS 3: 'For' Loops (and arrays) in JS (CodeAcademy)

Ace your homework & exams now with Quizwiz!

... ...

1. Introduction to 'For' Loops in JS 2. Practicing loops!

searching strings .push "methods" searching internet for methods / code

...

...

3. Arrays and Loops

arrays, like variables, store data except they can hold many elements (pieces of data) in the form of a list whereas a variable can only store one piece of data at a time. The position of the array is fixed, i.e. it does not change positions in memory and hence each element is fixed. e.g. you can't switch the 3rd element with 1st element, but rather store/replace what data is at a specific element. In JS, different elements in an array can contain different data types, so when you store data in a particular element, it must be of the right data type (or errors will occur). e.g. of array declaration var junk = ["Check","check...",1,2]; e.g. of printing an array console.log(junk); An array is indicated by brackets, [ ], and the elements of the array of separated by commas. write more later about how the name of the array is the memory address that the list ect.

Arrays

when the conditional test is always true and the loop continues on forever. this will cause your computer or program to crash.

infinite loop

"We are learning how to program because we don't want to do boring, repetitive work!"-Leng Lee loops let you iterate an algorithm for as long as a certain condition is true.

loops

To systematically access every element in an array a for loop can be used. the counter variable serves as a way to specify a particular element in the array with each iteration of the loop. e.g. var cities = ["Melbourne", "Amman", "Helsinki", "NYC", "LA", "Sandiego"]; for (var i = 0; i < cities.length; i++) { console.log("I would like to visit" + " " + cities[i]); } in this code, every element of the array is printed in combination with a string prior to it. *note how the counter starts at 0 so that the first element of the array is accessed. *note the use of cities.length for the conditional to end the loop when the last element has been accessed.

accessing all elements in an array with the for loop

Elements in an array are referred to by placing a number in brackets after the array name. The first element in an array is identified by 0, the second by 1, and so on... e.g. var junkData = ["Eddie Murphy", 49, "peanuts", 31]; console.log(junkData[3]); *this prints out 31, the fourth element of the array as indicated by junkData[3] in the above code.

accessing an element in an array

when .length is used with an array name, e.g. arrayName.length, the number of elements in the array is returned. e.g. var randomArray = ["test1", "test2", "test3"]; var x = randomArray.length; in the above code the number of elements in the array, 3, is stored in the variable x.

array.length

var array = [3 , 6, 2, 56, 32, 5, 89, 32]; var largest= 0; for(var i=0; i<array.length;i++) { if(array[i]>largest) { largest=array[i]; } } console.log(largest); the loop iterates as many times as there are element in the array. each time the loop iterates it checks to see if the current element is larger than the previous largest element; if it is, then that element is stored in the variable, largest.

e.g. of array, loops, and variable to find higher number in an array

for loops let you iterate an algorithm a set number of times. general form of for loop: for(state counter variable once; test counter each iteration with a condition; modify counter each iteration after the first iteration) { this block is iterated as long as the test conditional evaluates to true. } *clarification: if the test/conditional statement is true the first time, the block will run at least once before the counter is modified. e.g. of a for loop with detailed explanation: for (var i = 5; i < 51; i+=5) { console.log(i); } (var i = 5; - this code declares and initializes the counting variable that is tested each iteration and altered each iteration after the first iteration. i<51; - this is the condition that the counting variable must meet to continue iterating; if the conditional statement is false, then the loops ends. i+=5) - this is the part of the loop that modifies the counting variable for each iteration of the loop after the initial iteration of the block, i.e. the block will run at least once before the counter is modified (unless the initial test statement fails). {console.log(i);} - this is the code that is executed with each iteration of the loop. Notice there is *no semicolon* after the closing brace of a loop. *The flow of the for loop:* 1. The first thing the for loop does is state the counting variable. 2. next, it tests the counting variable with the conditional in 2nd statement in parentheses of the for loop. 3. next, it does one iteration of the block of code (between the { and the }) 4. next, it modifies the counting variable with the 3rd statement in the parentheses of the for loop. 5. next it tests the variable with a conditional again, iterate the block, modifies the counter, tests the counter, iterates the block, modifies the counter, tests the counter... and so on until the conditional statement that tests the counter variable is false-- then the loop ends. summary: declare counter>test counter>iterate block >modify>test>iterate>modify>test....until test fails

for loops

for these examples, assume that x has been declared as a var (e.g. var x;). each 2 lines is an equivalent line of code: x=x+1; x++; (operates after the statement) ++x; (operates before the statement) x=x-1; x--; (operates after the statement) --x; (operates before the statement) x+=5; x=x+5; x-=5; x=x-5; x*=5; x=x*5; x/=5; x=x/5;

shortcut syntax for modifying variables

text wrapping requires the include direcive, jshint multistr:true, and allows text wrapping for strings by using, / e.g. jshint multistr:true var randomString="at this point right HERE\ i just rapped text to the next line so the line\ isn't one long line"

text wrapping (edit and elaborate more later)


Related study sets

Chapter 15: Schizophrenia and Other Psychotic Disorders NCLEX

View Set

Struck-By & Caught-Between Accidents

View Set

Basic Concepts of Sensation and Perception

View Set

Quiz Chapter 6 Thinking and Intelligence (A2)

View Set

CH 13 Sustainable Cities SCI 1102

View Set

ADDING AND SUBTRACTING POLYNOMIALS

View Set

Vocabulary Workshop Level H units 1-6

View Set