Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="description" content="Whats are loops?">
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width">
- <title>Loops</title>
- </head>
- <body>
- <script id="jsbin-javascript">
- /* Loops
- There are many different kinds of loops, but they all essentially do the same thing: they repeat an action some number of times.
- The various loop mechanisms offer different ways to determine the start and end points of the loop.
- */
- // 1. for loop
- for(var i = 0; i < 10; i++){ //===> loops from 0 to 10//
- console.log(i); //===> prints all numbers in the loop//
- }
- for(var j = 10; j > 0; j--){
- console.log(j); //===> prints bacwards from 10 to 1//
- }
- // Loop through arrays
- function loopArray(myArray){
- for(var a = 0; a < myArray.length; a++){
- console.log(myArray[a]);
- }
- }
- loopArray(["david", 28, "pizza"]);
- function loopReverseArray(myArray){
- for(var b = myArray.length -1; b >= 0; b--){
- console.log(myArray[b]);
- }
- }
- loopReverseArray(["david", 28, "pizza"]);
- // 2. Loop through objects using for..in
- function objectKeys(myObject) {
- for (var prop in myObject) {
- console.log(myObject[prop]);
- }
- }
- objectKeys({name: "gabby", age: 27, food: "chips"})
- // 3. while loop
- var w = 7;
- while (w < 10) {
- console.log(w); //===> 7, 8, 9
- w++;
- }
- //the code in the loop will run as long as the variable (w) is less than 10
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">/* Loops
- There are many different kinds of loops, but they all essentially do the same thing: they repeat an action some number of times.
- The various loop mechanisms offer different ways to determine the start and end points of the loop.
- */
- // 1. for loop
- for(var i = 0; i < 10; i++){ //===> loops from 0 to 10//
- console.log(i); //===> prints all numbers in the loop//
- }
- for(var j = 10; j > 0; j--){
- console.log(j); //===> prints bacwards from 10 to 1//
- }
- // Loop through arrays
- function loopArray(myArray){
- for(var a = 0; a < myArray.length; a++){
- console.log(myArray[a]);
- }
- }
- loopArray(["david", 28, "pizza"]);
- function loopReverseArray(myArray){
- for(var b = myArray.length -1; b >= 0; b--){
- console.log(myArray[b]);
- }
- }
- loopReverseArray(["david", 28, "pizza"]);
- // 2. Loop through objects using for..in
- function objectKeys(myObject) {
- for (var prop in myObject) {
- console.log(myObject[prop]);
- }
- }
- objectKeys({name: "gabby", age: 27, food: "chips"})
- // 3. while loop
- var w = 7;
- while (w < 10) {
- console.log(w); //===> 7, 8, 9
- w++;
- }
- //the code in the loop will run as long as the variable (w) is less than 10
- </script></body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement