Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * LOOPS: WHILE, FOR< FOR-IN:
- *
- * 0. Loops allow us to iterate through all of the elements in our collection.
- * Loops allow up to iterate a block of code as often as needed. A standard for loop
- * has 3 parts: the initialized starting position, the point in which we want the loop to
- * stop, and the incrementor. We have to declare a variable for the index, which is usually
- * set to "i". For objects, we use a for-in loop, because the "in" aspect
- * allows us to pull the keys from an object. Lastly, a while loop will continue to execute
- * a block of code "while" the condition remains true.
- */
- //Looping over an Array, forwards and backwards:
- function printArrayValues(array) {
- for(var i = 0; i < array.length; i++) {
- console.log(array[i]);
- }
- }
- function printArrayValuesInReverse(array) {
- for(var i = array.length -1 ; i > -1; i--) {
- console.log(array[i]);
- }
- }
- //Looping over an Object, forwards and backwards: ( hint: keys = Object.keys(myObject); )
- function printObjectValues(object) {
- for(var prop in object) {
- console.log(object[prop]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement