Created
June 12, 2017 01:17
-
-
Save Ncreshon/093c924fd4e4db63aa7de4827510927d to your computer and use it in GitHub Desktop.
JS Bin[Loops: While, for, for-in]// source https://jsbin.com/bexagem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Loops// | |
| /* Loops are built-in features of JavaScript that allow | |
| * us to execute a block of code as many times as needed */ | |
| //for Loops// | |
| //There are 3 parts to a for loop.// | |
| //startng point // | |
| // ending point // | |
| //how much to increase or decrease by// | |
| var arr = ['1','2','3','4','5','6','7']; | |
| for(var i = 0; i < arr.length; i++ ) { | |
| console.log(arr[i]); | |
| } | |
| //prints '1','2','3'...// | |
| //Backwards// | |
| for(var i = arr.length - 1; i > -1; i--) { | |
| console.log(arr[i]); | |
| } | |
| // prints '7','6','5'...// | |
| for(var i = 1; i <= 10; i++ ) { | |
| console.log(i); | |
| } | |
| //prints 1-10// | |
| for(var i = 10; i >= 0; i--) { | |
| console.log(i); | |
| } | |
| // prints 10-0// | |
| //for in loops// | |
| var obj = {oldestName: "Christian Sky", | |
| babyName: "Harley Quinn", | |
| bonusName: "Tymia Nikol", | |
| momName: "Nicole", | |
| dadName: "Quentin",}; | |
| for(var names in obj){ | |
| console.log(obj[names]); //prints values// | |
| } | |
| for(var key in obj){ | |
| console.log(key); //prints keys// | |
| function objReverse(object) { | |
| var cache = []; | |
| for(var key in object) { | |
| cache.push(object[key]); | |
| } | |
| var cache = cache.reverse(); | |
| for(var i = 0; i < cache.length; i++){ | |
| console.log(cache[i]); | |
| } | |
| } | |
| } | |
| objReverse(obj); | |
| //while loops// | |
| var i = 1; | |
| while (i <= 25){ | |
| console.log(i); | |
| i++; | |
| } /// counts to 25 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment