Arrays Flashcards

1
Q

What is an array?

A

An array is a collection of data, and can be strings, numbers, booleans or a mix thereof.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
What will the following console.log return? 
const catNames = ['Clawdia', 'Special Agent Mittens', 'Pixel', 'Jennifur', 'Linux'];
console.log(catNames.length);
A

There are 5 items in the array, and count starts at 0.

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

Arrays always start at __

A

Zero

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

What method would can be used to tell us the value of the last index in an array?

A

The .length method

Ex: 
const dogNames = ['Kareem Abdul Ja-Bark', 'Mary Puppins', 'Salvador Dogi', 'Sherlock Bones', 'Bark Twain'];
console.log(dogNames[dogNames.length - 1]);

Will return ‘Bark Twain,’ the last name in the array.

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

What do .push and .pop do?

A

.push adds an item or items to the end of an array (think ‘the items are pushed into the array container’)

.pop removes an item or items from the end of the array (think “Pop! It’s gone!”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
const cheeses = ['Gouda', 'Cheddar', 'Gruyere', 'Brie', 'Parmesan'];
cheeses[0] = 'String';
console.log(cheeses); 

What are we doing here, and what will the console.log return?

A

We are reassigning the 0 place in the index to now read ‘String’ instead of ‘Gouda’.

It will return ‘String’, ‘Cheddar’, ‘Gruyere’, ‘Brie’, ‘Parmesan’

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

What do .shift and .unshift do?

A

.unshift will add a new index at the beginning (the front) of an array.

.shift will remove the first index in the array

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

What is a JavaScript method?

A

A method is an action that can be performed on an object.

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

True or False - the following declaration is a valid array?

let newArr = [1, 2, 3, “Hello World, 4.25, true];

A

True

An array can contain numbers, strings, booleans and floating point numbers (decimals)

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

What does the .slice method do?

A

.slice is a way to copy a given part of an array.

It is assigned using 2 values indicating from where we start and to where we end.

Important Note: the use of copy here is important. .slice() It does not change the original array.

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

let fishNames = [‘Tank Sinatra’, ‘Harley Fin’, ‘Bait’, ‘Kosher’, ‘Anne Chovy’, ‘DJ Great-White, ‘Squirt’];

let fishNameSlice = fishNames.slice(1, 3);

console.log(fishNameSlice);

What names will fistNameSlice return?

A

“Harley Fin” and “Bait”

The .slice method function will return the 1st until the 3rd index in the array, so in this case, it will create a new array with the names “Harley Finn” and “Bait”.

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

Describe the .splice() method.

What 2 functions can it do?

A

.splice() is a way to change the original array. It can add or remove items to the array.

Important Note: the use of change is important here. This method changes the array

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

let index = [1, 2, 3, 4, 5, 6, 7];

index. splice(3);
console. log(index);

What will this return?

A

“1, 2, 3”

Note: If the second parameter (telling the slice when to end) isn’t given it removes everything after that index.

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