Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. /*
  2. * LOOPS: WHILE, FOR< FOR-IN:
  3. *
  4. * 0. Loops allow us to iterate through all of the elements in our collection.
  5. * Loops allow up to iterate a block of code as often as needed. A standard for loop
  6. * has 3 parts: the initialized starting position, the point in which we want the loop to
  7. * stop, and the incrementor. We have to declare a variable for the index, which is usually
  8. * set to "i". For objects, we use a for-in loop, because the "in" aspect
  9. * allows us to pull the keys from an object. Lastly, a while loop will continue to execute
  10. * a block of code "while" the condition remains true.
  11. */
  12.  
  13. //Looping over an Array, forwards and backwards:
  14. function printArrayValues(array) {
  15. for(var i = 0; i < array.length; i++) {
  16. console.log(array[i]);
  17. }
  18. }
  19.  
  20. function printArrayValuesInReverse(array) {
  21. for(var i = array.length -1 ; i > -1; i--) {
  22. console.log(array[i]);
  23. }
  24. }
  25. //Looping over an Object, forwards and backwards: ( hint: keys = Object.keys(myObject); )
  26. function printObjectValues(object) {
  27. for(var prop in object) {
  28. console.log(object[prop]);
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement