Arrays Flashcards
Set the ‘mixture’ variable to an array with 3 string elements, 2 numbers and one boolean.
var mixture = [“a”, “b”, “c”, 1, 2, true];
Set the variable ‘guides’ that has placeholders for 42 elements but has no values stored in it.
var guides = new Array(42);
var myArray = [“sugar”, “rush”, “fix”, “it”, 3.14, 42];
Get the 3rd element in ‘myArray’ to the variable ‘thirdElementInArray’.
var thirdElementInArray = myArray[2];
var myArray = [“sugar”, “rush”, “fix”, “it”, 3.14, 42];
Set the 31st element in ‘myArray’ to the word ‘treehouse’.
myArray[30] = “treehouse”;
var spareWords = [“The”,”chimney”,”sweep’s”,”dog”];
Use a method on the ‘spareWords’ to set ‘firstWord’ to be the first word of the ‘spareWords’ array whilst simultaneously removing it from the beginning of the array.
var firstWord = spareWords.shift();
- removes and returns the first value in the array
var spareWords = [“The”,”chimney”,”sweep’s”,”dog”];
Use a method on the ‘spareWords’ to set ‘lastWord’ to be the last word of the ‘spareWords’ array whilst simultaneously removing it from the end of the array.
var lastWord = spareWords.pop();
- removes and returns the last value in the array
var spareWords = ["The","chimney","sweep's","dog"]; var saying = ["quick", "brown", "fox", "jumps", "over", "the", "lazy"]; var firstWord = spareWords.shift(); var lastWord = spareWords.pop();
Add the ‘firstWord’ variable to the beginning of the array ‘saying’.
saying. unshift(firstWord);
- prepends a value to the beginning of the array
var spareWords = ["The","chimney","sweep's","dog"]; var saying = ["quick", "brown", "fox", "jumps", "over", "the", "lazy"]; var firstWord = spareWords.shift(); var lastWord = spareWords.pop();
Add the ‘lastWord’ variable to the end of the array ‘saying’.
saying. push(lastWord);
- appends a value to the end of the array
var saying1 = [“The”, “quick”, “brown”, “fox”, “jumps”, “over”, “the”, “lazy”, “dog”];
Modify ‘saying1’ so it’s in the opposite order.
saying1.reverse();
var saying2 = [“The”, “quick”, “brown”, “fox”, “jumped”, “over”, “the”, “lazy”, “dog’s”, “back”];
Sort the ‘saying2’ array, so that the words are listed in length order. The shortest first. Use the ‘length’ property on the strings in the sort function.
saying2.sort(function(a, b) {
return a.length-b.length;
});
var first = ["The", "quick", "brown", "fox", "jumps"] var second = ["over", "the", "lazy"];
Set the variable ‘saying’ to the concatenation of the ‘first’ and ‘second’ arrays with the word “dog”.
var saying = first.concat(second).concat(“dog”);
- appends all of the values to the array in order
var first = ["The", "quick", "brown", "fox", "jumps"] var second = ["over", "the", "lazy"]; var saying = first.concat(second).concat("dog");
Set the variable ‘shortSaying’ to be a slice of the ‘saying’ array to include the strings of “quick”, “brown”, and “fox”.
var shortSaying = saying.slice(1, 4);
- returns a new array based on the array from index start to index end
var first = ["The", "quick", "brown", "fox", "jumps"] var second = ["over", "the", "lazy"]; var saying = first.concat(second).concat("dog");
Set the ‘sayingString’ variable to the string of all the strings in ‘saying’ joined together by spaces.
var sayingString = saying.join(“ “);
- returns a string with all values in array joined with the string separator
var saying = [“The”, “quick”, “brown”, “fox”, “jumps”, “over”, “the”, “lazy”, “dog”];
Use the ‘splice’ method to replace “quick” and “brown” from the ‘saying’ array and add “slow” and “red” in their place in that order.
saying.splice(1, 2, “slow”, “red”);
In the following code, what value would be stored in result?
var values = ["Treehouse", "Adventure", "Frog"]; var result = values[3];
undefined