Arrays Flashcards

1
Q

Set the ‘mixture’ variable to an array with 3 string elements, 2 numbers and one boolean.

A

var mixture = [“a”, “b”, “c”, 1, 2, true];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Set the variable ‘guides’ that has placeholders for 42 elements but has no values stored in it.

A

var guides = new Array(42);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

var myArray = [“sugar”, “rush”, “fix”, “it”, 3.14, 42];

Get the 3rd element in ‘myArray’ to the variable ‘thirdElementInArray’.

A

var thirdElementInArray = myArray[2];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

var myArray = [“sugar”, “rush”, “fix”, “it”, 3.14, 42];

Set the 31st element in ‘myArray’ to the word ‘treehouse’.

A

myArray[30] = “treehouse”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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.

A

var firstWord = spareWords.shift();

  • removes and returns the first value in the array
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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.

A

var lastWord = spareWords.pop();

  • removes and returns the last value in the array
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
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’.

A

saying. unshift(firstWord);

- prepends a value to the beginning of the array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
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’.

A

saying. push(lastWord);

- appends a value to the end of the array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

var saying1 = [“The”, “quick”, “brown”, “fox”, “jumps”, “over”, “the”, “lazy”, “dog”];

Modify ‘saying1’ so it’s in the opposite order.

A

saying1.reverse();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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.

A

saying2.sort(function(a, b) {
return a.length-b.length;
});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
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”.

A

var saying = first.concat(second).concat(“dog”);

  • appends all of the values to the array in order
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
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”.

A

var shortSaying = saying.slice(1, 4);

  • returns a new array based on the array from index start to index end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
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.

A

var sayingString = saying.join(“ “);

  • returns a string with all values in array joined with the string separator
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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.

A

saying.splice(1, 2, “slow”, “red”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

In the following code, what value would be stored in result?

var values = ["Treehouse", "Adventure", "Frog"];
var result = values[3];
A

undefined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the final value of the array values after this code is executed?

var values = ["Treehouse", "Island", "Adventure"];
values.splice(1, 1, "Jungle", "Ambassador");
A

[“Treehouse”, “Jungle”, “Ambassador”, “Adventure”]

17
Q

What piece of code would replace the value “Adventure” with “Island”?

var values = [“Treehouse”, “Adventure”, “Frog”];

A

values[1] = “Island”;

18
Q

What does the push( ) method do to an array?

A

it takes a value and appends it to the end of the array

19
Q

Given the following array, what would evaluate to the value “Treehouse”?

var values = [“Treehouse”, “Adventure”, “Frog”];

A

values[0];

20
Q

What is the the length of this array?

var values = [1, 2, [3, 4, 5], 6];

A

4

21
Q

What does the pop( ) method do to an array?

A

removes and returns the last value of that array

22
Q

Given the following code, how are array1 and array2 modified when concat is called?

var array1 = [1, 2, 3];
var array2 = [3, 4, 5];
var array3 = array1.concat(array2);
A

neither array1 nor array2 are modified after they are created

23
Q

When creating an array with the syntax new Array(argument), what is the argument passed to Array?

A

the initial length of the array