Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width">
- <title>JS Bin</title>
- </head>
- <body>
- <script id="jsbin-javascript">
- // LOOPS
- /* Loops are used for running code multiple times. we can loop over forward and backwards
- through arrays and objects. There are a few different types of loops, such as::
- FOR LOOP -- loops through CODE BLOCK
- FOR/IN LOOP -- loops through the properties of an OBJECT
- WHILE LOOP -- loops through code while coded conditions are TRUE
- DO/WHILE -- ALSO loops through code while coded conditions are TRUE
- */
- //heres a few examples
- var quote = "";
- for (var i = 0; i < 10; i++) {
- // var i = 0 shows where we will start with our loop
- //i < 10 shows how long the loop will loop, or where we will stop our loop
- //i++ is our increment, it shows us the count we will do as the loop occurs
- quote += i;
- }
- console.log(quote);
- //LOOPING FORWARDS AND BACKWARDS THROUGH AN ARRAY
- // FOR-IN LOOPS
- //here's how you loop through objects
- var laptop = { // first, we create an pbject
- color: "white",
- size: 'small',
- graphics: 'good',
- battery: 'great',
- type: 'lenovo',
- age: 'few munths',
- stickers: 'none'
- }
- for (var key in laptop) { //this says that for every key in the object laptop --
- console.log(laptop[key]); // log the laptop properties
- }
- var ki = Object.keys(laptop);// converts laptop object to an array
- console.log(ki); // logs the object's properties in an array
- console.log(ki.reverse());// reverses the objects properties
- //WHILE LOOP
- //the while loop states to do a particular code block while a condition is true
- var whippin = -5
- while (whippin < 10) {
- console.log(whippin);
- whippin++;
- }
- console.log(whippin);
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">// LOOPS
- /* Loops are used for running code multiple times. we can loop over forward and backwards
- through arrays and objects. There are a few different types of loops, such as::
- FOR LOOP -- loops through CODE BLOCK
- FOR/IN LOOP -- loops through the properties of an OBJECT
- WHILE LOOP -- loops through code while coded conditions are TRUE
- DO/WHILE -- ALSO loops through code while coded conditions are TRUE
- */
- //heres a few examples
- var quote = "";
- for (var i = 0; i < 10; i++) {
- // var i = 0 shows where we will start with our loop
- //i < 10 shows how long the loop will loop, or where we will stop our loop
- //i++ is our increment, it shows us the count we will do as the loop occurs
- quote += i;
- }
- console.log(quote);
- //LOOPING FORWARDS AND BACKWARDS THROUGH AN ARRAY
- // FOR-IN LOOPS
- //here's how you loop through objects
- var laptop = { // first, we create an pbject
- color: "white",
- size: 'small',
- graphics: 'good',
- battery: 'great',
- type: 'lenovo',
- age: 'few munths',
- stickers: 'none'
- }
- for (var key in laptop) { //this says that for every key in the object laptop --
- console.log(laptop[key]); // log the laptop properties
- }
- var ki = Object.keys(laptop);// converts laptop object to an array
- console.log(ki); // logs the object's properties in an array
- console.log(ki.reverse());// reverses the objects properties
- //WHILE LOOP
- //the while loop states to do a particular code block while a condition is true
- var whippin = -5
- while (whippin < 10) {
- console.log(whippin);
- whippin++;
- }
- console.log(whippin);</script></body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement