Mid Term study Arrays Flashcards

1
Q

Three ways to declare an Array?

A

let numbers = Array();
// OR
let numbers = new Array();
// OR
let numbers = [];

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

How to create an empty array with 3 elements

A

let numbers = Array(3);

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

How to add the number 10 to the Array below?

let numbers = [2, 4, 6, 8];

A

numbers[4] = 10;

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

What is output to the console from below:

let numbers = [2, 4, 6, 8];
console.log(numbers);

A

(4) [2, 4, 6 ,8]

(4) -> this is the array length
[2, 4, 6, 8] -> these are the array elements

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

What is the output to the console from below:

let numbers = [2, 4, 6, 8];
numbers.length = 2;
console.log(numbers.length);
console.log(numbers);

A

console.log(numbers.length);
outputs: 2

console.log(numbers);
outputs: [2, 4]

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

Is the below possible?:

let things = [24, “cat”, 8.9, true];
console.log(things);

A

Yes because Javascript can hold any data type

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

How to return index[2] from this array?

let numbers = [2, 4, 6, 8];

A

numbers.at(2)

.at() method will return the value at the specified index.

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

What is the console output:

let numbers = [2, 4, 6, 8];
console.log(numbers.at(-2))

A

output would be 6

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

Why are the .push() and .pop() array methods faster than the .shift() and .unshift() methods?

A

.pop() and .push() are faster than .shift()/.unshift() because .shift/unshift require you to re-index every other array element. While pop and push just remove or add to the end of the array.

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

What happens below:

let numbers = [2, 4, 6, 8];
console.log(numbers.pop()):
numbers.push(5);

A

console.log(numbers.pop()): - > pops the last element from the array(8 in this case)

numbers.push(5); - > pushes 5 to the end of the array

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

What happens below:

let numbers = [2, 4, 6, 8];
console.log(numbers.shift()):
numbers.unshift(5);

A

console.log(numbers.shift()): ->shifts the first element from the array(2 in this case)

numbers.unshift(5); - > shifts 5 to the start of the array

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

How do you Join the arrays below?

let numbers = [2, 4, 6, 8];
let more = [1, 2, 3];

A

numbers = numbers.concat(more);

Array is now:
[2, 4, 6, 8, 1, 2, 3]

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

How would you join all the elements in the array below into a string with each element spaced by “:”

let numbers = [2, 4, 6, 8];

A

let new = numbers.join(“:”):

new would equal the string
“2:4:6:8”

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

How would you copy part of an array, and copy from indexes 4, 5, 6 from the array below:

let numbers = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20];

A

let new = numbers.slice(4, 7);

.slice(start, end)

.slice is inclusive of the start but exclusive of the end.

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

Explain what the Array.splice() below does?

let numbers = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20];
let new = numbers.splice(4, 5);

A

Array.splice(start index, how much elements);

will start at index 4(value 10) and take from 5 indexes from there(inclusive). So new will equal [10, 12, 14, 16, 18]

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

Reverse the array below

let numbers = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20];

A

numbers.reverse();

17
Q

How to sort the array below?

let numbers = [10, -1, 20, 15, -20]

A

numbers.sort();

18
Q

What is the output below and explain:

let numbers = [8, -2, 1, 17, 18, -9, 10];
console.log(numbers.indexOf(17));
console.log(numbers.indexOf(99));
console.log(numbers.indexOf(17, 2));
console.log(numbers.indexOf(17, 4));

A

console.log(numbers.indexOf(17)); = returns index of 17 which would be 3

console.log(numbers.indexOf(99)); = 99 is not in array so returns -1

console.log(numbers.indexOf(17, 2)); = searches for index of 17 starting from index 2, will find it so returns index 3

console.log(numbers.indexOf(17, 4)); = searches for index of 17 starting from index 4, will not find it so returns -1

19
Q

What is array.lastindexOf() and how is it different from array.indexOf()?

A

array.lastindexOf() is the same except instead of moving forwards in indexes, it searches the array backwards

20
Q

Write syntax for a for:of loop of the array below

let numbers = [2, 4, 6];

A

for(const number of numbers){
// Use const if u dont wanna change element values
}
//OR
for(let number of numbers){
//Use if u want to change element values
}

21
Q

Write syntax for a for:in loop of the array below

let numbers = [2, 4, 6];

A

for (const index in numbers) {
console.log(numbers[index]);
}