L1: Collection Basics Flashcards

1
Q

What is a collection

A

A structured container.
A place to store data.

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

Method that returns portions of a string or array. Does not mutate

A

.slice

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

What does String.prototype.slice() return?

A

returns a segment of another string.

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

What happens if .slice indices are out of order like (3, 1)? (for arrays and strings)

A

Returns an empty string or empty array

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

What happens with negative slice indices like (-4, -2)
Still exclusive?

A

Works from the end (last index is -1)
Yes still exclusive

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

What happens if you omit the second argument in .slice

A

Captured index goes till the end

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

What is the index for the last element?
What 2 arguments do you use to ensure you capture the last element?

A

-1
Not 2 arguments. not 0 or -0. Just use 1 argument

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

With Array.prototype.slice(), what happens if you have no arguments? What does it return? Does it return a new array or a reference to the original?

A

A copy of the original array
new array

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

What happens if you have no arguments with String.prototype.slice()?

A

Returns the original string

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

What happens when you try to reference an array/string index outside what’s there?
How about negative indices? (strings vs arrays?)as with anArray[-3]

A

Undefined
Undefined
Not the value undefined, but the actual absence of the value.

With arrays, -1 is viewed as a key. undefined If it’s not there, or the value if it is.

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

How do you distinguish between a non object key and an object key with the value undefined?

A

Object.prototype.hasOwnProperty()
or
Object.keys(obj).includes(‘key to test’)

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

What does Object.prototype.hasOwnProperty() do?
Does the argument have to be a string?

A

Returns boolean if an object has the argument key

No, it coerces it into a string.

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

What happens with Object.keys(arr) on arr = [‘foo’, ‘bar’, ‘qux’]

A

[ ‘0’, ‘1’, ‘2’,}

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

What happens with arr[-1] = 374;

A

It creates a key ‘-1’

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

Increment the value of an array element
What happens if you try to increment a non-index?

A

let numbers = [1, 2, 3, 4]
numbers[0] = numbers[0] + 1;
or
arr[3] += 1;

Creates a NaN in the array. Basically assigns a NaN to that index of the array.

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

Reassign a character of a string

A

Can’t do this
str[0] = ‘B’;
although it won’t throw an error (for reasons we’ll learn later)

Primitives are immutable.
Have to reaassign.

7
Q

What is logged?
let arr = [‘ele1’];
arr[10] = ‘ele2’;
A
console.log(arr.length);

B
arr.forEach(ele => console.log(ele));

C
for (let element of arr) {
console.log(element);
}

D
For (let key in arr) {
console.log(arr[key]);
}
?

A

11 (the length)

for each
ele1
ele2

for element of
ele1
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
ele2

for key in
ele1
ele2

8
Q

What’s an array called when it has some empty elements?

A

a sparse array

9
Q

Do forEach or for of iterate over sparse items

A

foreach doesn’t
for of does