Skip to content

Instantly share code, notes, and snippets.

@pgainda
pgainda / for_loop_with_increment_more_than_one
Created May 7, 2013 19:00
To increase i by more than just one, use i += number. However, you can't use i + number for this. The increase won't be saved back into i. i will always be the starting value and never satisfy the second loop parameter. This will make the loop run forever and crash your browser
var i;
for (i = 2; i <= 6; i+=2) {
console.log(i);
}
var i;
var animals = ["cat", "dog", "ferret"];
// Loop goes here
for (i=0;i<animals.length;i++){
console.log(animals[i]);
}
@pgainda
pgainda / faster_array_loop
Created May 7, 2013 19:14
animals.length will be read only once before our loop starts printing them. This makes our new code a little faster than what we had before.
var i;
var animals = ["cat", "dog", "ferret"];
var length = animals.length;
// Loop goes here
for (i=0;i<length;i++){
console.log(animals[i]);
}
@pgainda
pgainda / loop_through_string
Created May 7, 2013 19:17
Text strings are fundamentally arrays of characters. We can get individual characters from a string the same way we do elements of an array: string[indexNumber].
var word = "code";
// Loop goes here
for(i=0;i<word.length;i++){
console.log(word[i]);
}
@pgainda
pgainda / substring_loop_characters
Created May 8, 2013 15:18
To make the subset of the string include the character at the start index, set i equal to start in the first loop parameter. To make the subset include the character at the end index, use the <= operator to compare i and end in the second loop parameter. Since characters are numbered by ones, increment i by one in the third loop parameter.
// Define substring here
function subString(input, start, end){
var i;
this.input = input;
this.start = start;
this.end = end;
for(i=start;i<=end;i++){
console.log(input[i]);
}
var name = "John Doe";
function getFirstName (fullName){
var i=0;
var firstName ="";
var next = fullName[0];
var length=fullName.length;
while(i<length && next!==" "){
firstName+=fullName[i];
i++;
var person = {
name: "Morgan Jones",
telephone: "(650) 777 - 7777",
email: "morgan.jones@example.com"
};
for (var propertyName in person) {
console.log(propertyName +": "+ person[propertyName]);
}
@pgainda
pgainda / for_loop_double
Created May 13, 2013 08:14
Prints 1-5 three times.
for (i=1;i<4;i++){
for(j=1;j<6;j++){
console.log(j);
}}
var table, rows, i;
table = [
["Person", "Age", "City"],
["Sue", 22, "San Francisco"],
["Joe", 45, "Halifax"]
];
rows = table.length;
for (i = 0; i < rows; i++) {
console.log(table[i].join(" "));
@pgainda
pgainda / break_loop
Created May 18, 2013 14:14
Let's say we have a loop and there's some condition that makes us want to immediately stop the loop. There's a handy statement called break that lets us do just that. It works for both for and while loops, but not for recursion. It's similar to return (which lets you end a function), but is specifically for loops only.
while (true) {
console.log("I only print once!");
break;
}