Skip to content

Instantly share code, notes, and snippets.

@bashbaugh
Last active February 26, 2019 01:10
Show Gist options
  • Save bashbaugh/d8bdf792743b12f2388f2f1fb1170297 to your computer and use it in GitHub Desktop.
Save bashbaugh/d8bdf792743b12f2388f2f1fb1170297 to your computer and use it in GitHub Desktop.
// i += 1 is a shorter way of writing i = i + 1
for (i = 0; i < 5; i += 1)
{ // <-- the code block starts here
// Console.log expects a string, not a number
// so if we are going to log i we need to turn it into a string first
// using the toString function of the number:
let string_i = i.toString()
// Now we can log i:
console.log("Right now, i is " + string_i);
} // <-- the code block ends here
console.log("Next Example!")
let ages = [16, 87, 33, 12];
// This time I'll use a different variable: j instead of i, just to show that you can.
// array.length gets the length (number of values) of an array
// so ages.length returns 4 because there are four numbers in ages, so this loop will run four times:
// which is perfect because there are only four ages to log:
for (j = 0; j < ages.length; j += 1)
{
// Here we use j, which is the same j as the one above that keeps track of the index for the for loop
// so it will increment by 1 every time this loop is run.
// here we use j as the index for the age we want to retreive from the array ages:
// and because j increments each time the loop is run, the next age will be logged each time.
// like this: first age[0], then age[1], then age[2], then age[4].
// First we need to turn the age, which is a number, into a string:
let string_age = ages[j].toString()
// And then we can log it:
console.log(string_age)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment