let fibonacci = [1, 1, 2, 3, 5, 8, 13];
console.log(fibonacci[2]);
How would you explain this code to someone else? Use your own words.
solution
In line 1, we create an array of numbers called fibonacci. In line 2 we print out the item at index 2.
What is the number printed out in the second line of code?
solution
The item at index 2 happens to also be `2`.
What is the syntax for adding a new item to the end of the array?
solution
`fibonacci.push(newNumber);` where `newNumber` is a number. You can read about `.push()` and other the Array methods on MDN.
let sisters = ["paula", "tina", "mara"];
let brothers = ["juan", "edgar"];
let siblings = sisters.concat(brothers);
let eldest = siblings[3];
What is the value of siblings?
solution
In line 3, we use the `Array.concat()` method to combine the two arrays. Thus, the value of siblings is `["paula", "tina", "mara", "juan", "edgar"]`. You can read about `.concat()` and other the Array methods on MDN.
What is the value of eldest?
solution
In line 4 we create a variable called `eldest` which is equal to the item in `siblings` at index 3, i.e. `"mara"`.
let shoppingList = ["eggs", "milk", "bread", "tomato sauce", "garlic"];
shoppingList[1] = "soy milk";
What is happening in line 2? What is the new value of shoppingList?
solution and hint
In line 2 we set a new value at index 1 of `shoppingList`. Thus `shoppingList` now looks like this: `["eggs", "soy milk", "bread", "tomato sauce", "garlic"]`.
How can we remove the last two items from the array? In otherwords, we want to modify shoppingList to be ["eggs", "milk", "bread"].
hint
HINT: There are several ways to do this. From a google search of "remove last two items of array js", the first results are Array.pop() and Array.splice(). Try searching these before you look at final solution.
solution
Using `Array.pop()`: .pop() removes the last item of an array, so you could have to repeat `shoppingList.pop();` twice.
Using Array.splice(): .splice() has several options for syntax. Here we can use two parameters which represent starting index and delete count like so: shoppingList.splice(startIndex, deleteCount). Since we want to delete the last 2 items, we have to start at index 2 and delete 2 items: shoppingList.splice(2, 2);
let todoArray = ("walk the dog", "cook lunch", "make the bed", "vacuum");
todoArray.push("water plants");
console.log(todoArray[6]);
What is wrong with this code?
solution
In the line 1, we see incorrect array declaration with parentheses `()` instead of square brackets `[]`. The last line returns an error because we are trying to print an item at index 6. But the highest index is 5, so this is undefined.
What changes can you make to fix it?
solution
Add square brackets and use an index that is within the range of this array.
let todoArray = ["walk the dog", "cook lunch", "make the bed", "vacuum"];
todoArray.push("water plants");
console.log(todoArray[5]);
a) Create an array of strings called familyArr. Every item is a name of a family member. The array should have length 6. The family member at index 2 should have the same name as the family member at index 5.
example solution
let familyArr = ["Gina", "Farah", "Ari", "Stephen", "Ari", "Ralph"];
b) Create a variable called daughter and set it equal to one of the familyArr items.
example solution
let familyArr = ["Gina", "Farah", "Ari", "Stephen", "Ari", "Ralph"];
let daughter = familyArr[0];
c) Create a variable that is equal to familyArr index 3. You can name this variable whatever you like.
example solution
let familyArr = ["Gina", "Farah", "Ari", "Stephen", "Ari", "Ralph"];
let daughter = familyArr[0];
let grandfather = familyArr[3];