Skip to content

Instantly share code, notes, and snippets.

@LindseyCason
Created July 24, 2017 04:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LindseyCason/2559a64cc80d6b146287a302f068024c to your computer and use it in GitHub Desktop.
Save LindseyCason/2559a64cc80d6b146287a302f068024c to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/hofabaz
<!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:
// In JavaScript, Loops are a way to do something quick, easy and
// repeatedly. There are multiple iteration statments available. All of
// the iteration statements essentially do the same thing; repeat an
// action a specific number of times. Some situations work better with
// one type of loop over the other types.
// For Loop:
// A for loop repeats until a specific condition evaluates to false.
// The syntax for a for loop is:
// for(initialExpression; condition; increment)
// statement
// The initial expression indicates where the loop will start couting.
// The condition expression is then evaluated and if it is true, the loop
// statement will execute. If the condition is false, the for loop
// ends and the count will loop back to the top and evaluate again.
// The next time it evaluates, it will increment/decrement according to the
// increment/decrement expression indicated.
// ex.
for(i = 0; i<4; i++){
console.log ("This loop ran " +i+ " times." );
}
//returns:
// "This loop ran 0 times."
// "This loop ran 1 times."
// "This loop ran 2 times."
// "This loop ran 3 times."
//You can also run the for loop backwards by reordering the statements
//in your for loop. You change where you want your loop to
// end and start, then you change your increment (i++) to a
// decrement(i--).
//ex.
for (i<=5; i >= 0; i--){
console.log ("Backwards loop goes down " +i+ " times!");
}
//returns:
// "Backwards loop goes down 4 times!"
// "Backwards loop goes down 3 times!"
// "Backwards loop goes down 2 times!"
// "Backwards loop goes down 1 times!"
// "Backwards loop goes down 0 times!"
// You use a for loop when you know how many times you want your
// loop to run because you can control when you want it to stop.
// You can also use a for loop to loop over an array of items. You do this
// by using the .length operator to indicate where you want the loop
// to end in the array. Most of the time you want th e loop to go through
// the entire array. First indicate your array and then the for loop after.
// ex.
var myArray = [1,2,3,4,5];
for(i=0; i <= myArray.length-1; i++) {
console.log ("Array loop # " +i+ "!");
}
// returns:
// "Array loop # 0!"
// "Array loop # 1!"
// "Array loop # 2!"
// "Array loop # 3!"
// "Array loop # 4!"
// You can also loop backwards over an array by reordering the statements in your
// for loop and then changing your increment to decrement.
var myArray2 = [6,7,8,9,0];
for(var x = myArray2.length-1; x>= 0; x--) {
console.log ("Backwards array loop " +x+ "!");
}
// returns:
// "Backwards array loop 4!"
// "Backwards array loop 3!"
// "Backwards array loop 2!"
// "Backwards array loop 1!"
// "Backwards array loop 0!"
// For In Loop:
// Another type of loop is the for in loop. This type of loop statement
// interates a specified variable over all the properties of an object.
// For each property, the specified statement is executed. The syntax
// looks like this:
var text = "";
var person = {
nameFirst: "Lindsey",
nameLast: "Cason",
age: 101,
job: "Student",
animals: 3,
car: "Nissan"
}
for (var x in person) {
text = text + " " + person[x];
}
console.log(text);
// returns:
// " Lindsey Cason 101 Student 3 Nissan"
console.log(Object.keys(person));
//returns the name of the keys:
// ["nameFirst", "nameLast", "age", "job", "animals", "car"]
// Objects do not have a specific order, therefore you can not just
// reverse the for in loop. What you must do is push the objects into
// an array and then loop over them as we stated above using the
// for loop.
// ex.
var arr = [];
var person1 = {
nameFirst: "Coco",
nameLast: "Pritchett",
age: 75,
job: "Welder",
animals: 3,
car: "Juke"
}
for (var key in person1) {
arr.push(key);
}
for (var i=arr.length-1; i>=0; i--) {
console.log(arr[i]);
}
//returns the keys in reverse order:
// "car"
// "animals"
// "job"
// "age"
// "nameLast"
// "nameFirst"
//Another type of loop is the while loop. This loop executes its statements
//as long a specified condition evaluates to true. You must set your counter
//first. If you do not set a condition in your parentheses that will eventually
//evaluate to false, you will have an infinite loop. You place your incrementing
//inside of your curly brackets.
//ex.
var i = 0;
while(i <=5) {
console.log("This is number " + i + "!");
i++;
}
// returns:
// "This is number 0!"
// "This is number 1!"
// "This is number 2!"
// "This is number 3!"
// "This is number 4!"
// "This is number 5!"
</script>
<script id="jsbin-source-javascript" type="text/javascript">// Loops:
// In JavaScript, Loops are a way to do something quick, easy and
// repeatedly. There are multiple iteration statments available. All of
// the iteration statements essentially do the same thing; repeat an
// action a specific number of times. Some situations work better with
// one type of loop over the other types.
// For Loop:
// A for loop repeats until a specific condition evaluates to false.
// The syntax for a for loop is:
// for(initialExpression; condition; increment)
// statement
// The initial expression indicates where the loop will start couting.
// The condition expression is then evaluated and if it is true, the loop
// statement will execute. If the condition is false, the for loop
// ends and the count will loop back to the top and evaluate again.
// The next time it evaluates, it will increment/decrement according to the
// increment/decrement expression indicated.
// ex.
for(i = 0; i<4; i++){
console.log ("This loop ran " +i+ " times." );
}
//returns:
// "This loop ran 0 times."
// "This loop ran 1 times."
// "This loop ran 2 times."
// "This loop ran 3 times."
//You can also run the for loop backwards by reordering the statements
//in your for loop. You change where you want your loop to
// end and start, then you change your increment (i++) to a
// decrement(i--).
//ex.
for (i<=5; i >= 0; i--){
console.log ("Backwards loop goes down " +i+ " times!");
}
//returns:
// "Backwards loop goes down 4 times!"
// "Backwards loop goes down 3 times!"
// "Backwards loop goes down 2 times!"
// "Backwards loop goes down 1 times!"
// "Backwards loop goes down 0 times!"
// You use a for loop when you know how many times you want your
// loop to run because you can control when you want it to stop.
// You can also use a for loop to loop over an array of items. You do this
// by using the .length operator to indicate where you want the loop
// to end in the array. Most of the time you want th e loop to go through
// the entire array. First indicate your array and then the for loop after.
// ex.
var myArray = [1,2,3,4,5];
for(i=0; i <= myArray.length-1; i++) {
console.log ("Array loop # " +i+ "!");
}
// returns:
// "Array loop # 0!"
// "Array loop # 1!"
// "Array loop # 2!"
// "Array loop # 3!"
// "Array loop # 4!"
// You can also loop backwards over an array by reordering the statements in your
// for loop and then changing your increment to decrement.
var myArray2 = [6,7,8,9,0];
for(var x = myArray2.length-1; x>= 0; x--) {
console.log ("Backwards array loop " +x+ "!");
}
// returns:
// "Backwards array loop 4!"
// "Backwards array loop 3!"
// "Backwards array loop 2!"
// "Backwards array loop 1!"
// "Backwards array loop 0!"
// For In Loop:
// Another type of loop is the for in loop. This type of loop statement
// interates a specified variable over all the properties of an object.
// For each property, the specified statement is executed. The syntax
// looks like this:
var text = "";
var person = {
nameFirst: "Lindsey",
nameLast: "Cason",
age: 101,
job: "Student",
animals: 3,
car: "Nissan"
}
for (var x in person) {
text = text + " " + person[x];
}
console.log(text);
// returns:
// " Lindsey Cason 101 Student 3 Nissan"
console.log(Object.keys(person));
//returns the name of the keys:
// ["nameFirst", "nameLast", "age", "job", "animals", "car"]
// Objects do not have a specific order, therefore you can not just
// reverse the for in loop. What you must do is push the objects into
// an array and then loop over them as we stated above using the
// for loop.
// ex.
var arr = [];
var person1 = {
nameFirst: "Coco",
nameLast: "Pritchett",
age: 75,
job: "Welder",
animals: 3,
car: "Juke"
}
for (var key in person1) {
arr.push(key);
}
for (var i=arr.length-1; i>=0; i--) {
console.log(arr[i]);
}
//returns the keys in reverse order:
// "car"
// "animals"
// "job"
// "age"
// "nameLast"
// "nameFirst"
//Another type of loop is the while loop. This loop executes its statements
//as long a specified condition evaluates to true. You must set your counter
//first. If you do not set a condition in your parentheses that will eventually
//evaluate to false, you will have an infinite loop. You place your incrementing
//inside of your curly brackets.
//ex.
var i = 0;
while(i <=5) {
console.log("This is number " + i + "!");
i++;
}
// returns:
// "This is number 0!"
// "This is number 1!"
// "This is number 2!"
// "This is number 3!"
// "This is number 4!"
// "This is number 5!"
</script></body>
</html>
// Loops:
// In JavaScript, Loops are a way to do something quick, easy and
// repeatedly. There are multiple iteration statments available. All of
// the iteration statements essentially do the same thing; repeat an
// action a specific number of times. Some situations work better with
// one type of loop over the other types.
// For Loop:
// A for loop repeats until a specific condition evaluates to false.
// The syntax for a for loop is:
// for(initialExpression; condition; increment)
// statement
// The initial expression indicates where the loop will start couting.
// The condition expression is then evaluated and if it is true, the loop
// statement will execute. If the condition is false, the for loop
// ends and the count will loop back to the top and evaluate again.
// The next time it evaluates, it will increment/decrement according to the
// increment/decrement expression indicated.
// ex.
for(i = 0; i<4; i++){
console.log ("This loop ran " +i+ " times." );
}
//returns:
// "This loop ran 0 times."
// "This loop ran 1 times."
// "This loop ran 2 times."
// "This loop ran 3 times."
//You can also run the for loop backwards by reordering the statements
//in your for loop. You change where you want your loop to
// end and start, then you change your increment (i++) to a
// decrement(i--).
//ex.
for (i<=5; i >= 0; i--){
console.log ("Backwards loop goes down " +i+ " times!");
}
//returns:
// "Backwards loop goes down 4 times!"
// "Backwards loop goes down 3 times!"
// "Backwards loop goes down 2 times!"
// "Backwards loop goes down 1 times!"
// "Backwards loop goes down 0 times!"
// You use a for loop when you know how many times you want your
// loop to run because you can control when you want it to stop.
// You can also use a for loop to loop over an array of items. You do this
// by using the .length operator to indicate where you want the loop
// to end in the array. Most of the time you want th e loop to go through
// the entire array. First indicate your array and then the for loop after.
// ex.
var myArray = [1,2,3,4,5];
for(i=0; i <= myArray.length-1; i++) {
console.log ("Array loop # " +i+ "!");
}
// returns:
// "Array loop # 0!"
// "Array loop # 1!"
// "Array loop # 2!"
// "Array loop # 3!"
// "Array loop # 4!"
// You can also loop backwards over an array by reordering the statements in your
// for loop and then changing your increment to decrement.
var myArray2 = [6,7,8,9,0];
for(var x = myArray2.length-1; x>= 0; x--) {
console.log ("Backwards array loop " +x+ "!");
}
// returns:
// "Backwards array loop 4!"
// "Backwards array loop 3!"
// "Backwards array loop 2!"
// "Backwards array loop 1!"
// "Backwards array loop 0!"
// For In Loop:
// Another type of loop is the for in loop. This type of loop statement
// interates a specified variable over all the properties of an object.
// For each property, the specified statement is executed. The syntax
// looks like this:
var text = "";
var person = {
nameFirst: "Lindsey",
nameLast: "Cason",
age: 101,
job: "Student",
animals: 3,
car: "Nissan"
}
for (var x in person) {
text = text + " " + person[x];
}
console.log(text);
// returns:
// " Lindsey Cason 101 Student 3 Nissan"
console.log(Object.keys(person));
//returns the name of the keys:
// ["nameFirst", "nameLast", "age", "job", "animals", "car"]
// Objects do not have a specific order, therefore you can not just
// reverse the for in loop. What you must do is push the objects into
// an array and then loop over them as we stated above using the
// for loop.
// ex.
var arr = [];
var person1 = {
nameFirst: "Coco",
nameLast: "Pritchett",
age: 75,
job: "Welder",
animals: 3,
car: "Juke"
}
for (var key in person1) {
arr.push(key);
}
for (var i=arr.length-1; i>=0; i--) {
console.log(arr[i]);
}
//returns the keys in reverse order:
// "car"
// "animals"
// "job"
// "age"
// "nameLast"
// "nameFirst"
//Another type of loop is the while loop. This loop executes its statements
//as long a specified condition evaluates to true. You must set your counter
//first. If you do not set a condition in your parentheses that will eventually
//evaluate to false, you will have an infinite loop. You place your incrementing
//inside of your curly brackets.
//ex.
var i = 0;
while(i <=5) {
console.log("This is number " + i + "!");
i++;
}
// returns:
// "This is number 0!"
// "This is number 1!"
// "This is number 2!"
// "This is number 3!"
// "This is number 4!"
// "This is number 5!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment