Tracking Multiple Items with Arrays Flashcards
The script.js file contains an array called guestList. It contains a list of names. Add two new names to the end of the list using the array method you learned in the previous video.
var guestList = [‘Sandra’, ‘Omar’, ‘Magnus’, ‘Becky’];
Now, add 3 names to the beginning of the list using the other array method you learned in the last video.
guestList.push(“Name”, “Name”);
guestList.unshift(“Name”, “Name”, “Name”);
What array method adds an item to the end of an array?
push()
Complete the code below to store the number of items in the array scores into the variable totalScores:
var scores = [76, 79, 85, 87, 89 , 90, 99];
var totalScores = scores._____ ;
length
What array method returns the first item from an array and removes it from the array?
shift()
Finish the code below to add the value 23 to the beginning of the array ages:
ages._____ ( _____ );
unshift, 23
The orderQueue array contains a list of customer orders. Create a new variable named shipping – remove the first item from the array and place it in the shipping variable.
var orderQueue = [‘1XT567437’,’1U7857317’,’1I9222528’];
Now create a new variable named cancelled. Remove the last item from the orderQueue array and store it in the variable cancelled.
var shipping = orderQueue.shift(); var cancelled = orderQueue.pop();
Use a for or while loop to iterate through the values in the temperatures array from the first item – 100 – to the last – 10. Inside the loop, log the current array value to the console.
var temperatures = [100,90,99,80,70,65,30,10];
for(var i = 0; i \< temperatures.length; i++) { console.log(temperatures[i]); }
Use the array method that combines all of the items in an array into a single string. In the final string, the array items should be separated by a comma AND a space. Finally, log the final string value to the console.
var months = [‘January’, ‘February’, ‘March’, ‘April’, ‘May’, ‘June’, ‘July’, ‘August’, ‘September’, ‘October’, ‘November’, ‘December’];
console.log(months.join(“, “));
In this code challenge you need to create a two-dimensional array – an array of arrays. Start by creating an empty array literal named coordinates.
A two-dimensional Array is an array of arrays. Add 1 array to the coordinates array. That new array should have two items in it.
Add three more arrays to the coordinates array. The coordinates array should have 4 arrays in it – each of those arrays should have 2 items in it.
var coordinates = [
[“item”, “item”],
[“item”, “item”],
[“item”, “item”],
[“item”, “item”]
];
Finish the code by adding the array method that combines all of the items in an array into a single string.
var students = [“Sally”, “Joan”, “Raphael”, “Sam”]; var message = “Hello “ + students._____(“, “);
join
The following code should print out each item in the array times, but there’s one part is missing. Finish the code to log out each array item to the console:
var times = [1.22, 1.75, 2.10, 2.55];
for (var i=0; i < times.length; i += 1 ) {
console.log(times[_____]);
}
i
What array method combines one array with another to create a new array?
concat();
Say you have two arrays – oldScores and newScores and you want to combine those two into a new array named allScores. Complete the code so that the new array first has the old scores listed followed by the new scores:
var allScores = _____.concat(_____);
oldScores, newScores
When the following code runs what will appear in the JavaScript console?
var temperatures = [76.3, 44.9, 56, 89.8, 100.2]; console.log( temperatures.indexOf( 89.8 ) );
3