An array is like a variable, except it contains multiple values, not just one. Arrays are easy to spot, because they're stuffed between square brackets []. An array is defined just like a variable and can contain all the jiggly bits you want - numbers, strings, booleans and possibly long pieces of code that provide a value like functions or more. This is a simple example of an array:
var myArray = [1,true,"tree",4,5,"sticks"];
When we print an array to the screen what you see is what you get, square brackets and all, so we need a way to select which part of the array we'd like to display. We do that by logging or returning a column in the array. Just like selecting characters in a substring we start at 0; the first position. For example:
0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|
1 | true | "tree" | 4 | 5 | "sticks" |
To print out the string "tree" in this example we need to print the third column, or column 2.
Ex. `console.log(myArray[2]);
For Loops and Arrays
Using a for loop to print out all the variables in an array should be easy as pie. Here's a small example.
var fruit = ["Apple","Apricot","Avocado","Banana","Bilberry","Blackberry","Blackcurrant","Blueberry","Boysenberry"];
for (var i = 0; i < fruit.length; i++) {
console.log("Some fruits I like are " + fruit[i]);
}
Let's see what that does.
- First we define our array as
var fruit = [];
- We create a for loop with a start of
i = 0
- the first column's number - Then we create an end point where
i
is smaller than the length of the our array. In this case we have 9 fruit, so the array's length is 9 - that's clever,.length
is counting the number of items in the array, not the number of characters. Since the first column is 0 the last column is 8, conveniently 1 smaller than the length of the array. - We then create a control which adds 1 to our start point on each loop.
- We print the result to the console on each loop for example fruit[0], fruit[1], fruit[2], and so forth.
That seems pretty straight forward, and useful.
Test run
To see whether I get for loops I created this little piece of code. It's probably not very elegant, but it works.
var retry = 0;
for (retry; retry < 1; retry) {
// Find the users's name
var name = prompt("What is your name?");
// Create a list of possible new names
var newName = ["Alida", "Earnestine", "Jacklyn", "Tania", "Toney", "Danuta", "King", "Laine", "Clelia", "Nora", "Wade", "Zack", "Leland", "Emilee", "Jeremy", "Desire", "Catherina", "Nereida", "Anya", "Sondra"];
// Create a variable from the number of characters in the user's name.
var nameLength = name.length;
if (nameLength < 8) {
nameDifference = 0;
for (nameLength; nameLength < 8; nameLength++) {
nameDifference = nameDifference + 1;
}
newName = newName[nameDifference];
confirm("We think you need a better name. It'll be " + newName + ".");
console.log("We think you need a better name. It'll be " + newName + ".");
}
else {
nameDifference = 0;
for (nameLength; nameLength > 4; nameLength--) {
nameDifference = nameDifference + 1;
}
newName = newName[nameDifference];
confirm("We think you need a different name. It'll be " + newName + ".");
console.log("We think you need a different name. It'll be " + newName + ".");
}
var preference = prompt("Do you like your new name? Yes or No?");
if (preference === "Yes"||preference === "Y"||preference === "y") {
confirm("Wonderful, we're rather fond of " + newName + " ourselves.");
console.log("Wonderful, we're rather fond of " + newName + " ourselves.");
retry = 1;
}
else {
confirm("That's just sad, " + name + ". We kind of liked " + newName + " ourselves.");
console.log("That's just sad, " + name + ". We kind of liked " + newName + " ourselves.");
var retry = prompt("Would you like to try again? Yes or No?");
if (retry === "Yes"||retry === "Y"||retry === "y") {
retry = 0;
}
else {
retry = 1;
}
}
}
confirm("Thank you for playing.");
console.log("Thank you for playing.");