forked from GitbookIO/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd0b6fd
commit 1263801
Showing
4 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Arrays | ||
|
||
Arrays are a fundamental part of programming. An array is a list of data. We can store a lot of data in one variable, which makes our code more readable and easier to understand. It also makes it much easier to perform functions on related data. | ||
|
||
The data in arrays are called **elements**. | ||
|
||
Here is a simple array: | ||
```javascript | ||
// 1, 1, 2, 3, 5, and 8 are the elements in this array | ||
var numbers = [1, 1, 2, 3, 5, 8]; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Indicies | ||
|
||
So you have your array of data elements, but what if you want to access a specific element? That is where indicies come in. An **index** refers to a spot in the array. Indicies logically progress one by one, but it should be noted that the first index in an array is 0, as it is in most languages. Brackets [] are used to signify you are referring to an index of an array. | ||
|
||
```javascript | ||
// This is an array of strings | ||
var fruits = ["apple", "banana", "pineapple", "strawberry"]; | ||
|
||
// We set the variable banana to the value of the second element of | ||
// the fruits array. Remember that indicies start at 0, so 1 is the | ||
// second element. Result: banana = "banana" | ||
var banana = fruits[1]; | ||
``` | ||
--- | ||
|
||
Define the variables using the indices of the array | ||
|
||
```js | ||
var cars = ["Mazda", "Honda", "Chevy", "Ford"] | ||
var honda = | ||
var ford = | ||
var chevy = | ||
var mazda = | ||
``` | ||
|
||
```js | ||
var cars = ["Mazda", "Honda", "Chevy", "Ford"] | ||
var honda = cars[1]; | ||
var ford = cars[3]; | ||
var chevy = cars[2]; | ||
var mazda = cars[0]; | ||
``` | ||
|
||
```js | ||
assert(honda === "Honda"); | ||
assert(ford === "Ford"); | ||
assert(chevy === "Chevy"); | ||
assert(mazda === "Mazda"); | ||
``` | ||
|
||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Length | ||
|
||
Arrays have a property called length, and it's pretty much exactly as it sounds, it's the length of the array. | ||
|
||
```javascript | ||
var array = [1 , 2, 3]; | ||
|
||
// Result: l = 3 | ||
var l = array.length; | ||
``` | ||
|
||
--- | ||
|
||
Define the variable a to be the number value of the length of the array | ||
|
||
```js | ||
var array = [1, 1, 2, 3, 5, 8]; | ||
var l = array.length; | ||
var a = | ||
``` | ||
|
||
```js | ||
var array = [1, 1, 2, 3, 5, 8]; | ||
var l = array.length; | ||
var a = 6; | ||
``` | ||
|
||
```js | ||
assert (a === 6) | ||
``` | ||
--- |