Basic data structures Flashcards

1
Q

What does “zero-indexed” mean for an array?

A

The first element of an array is actually at the zeroth position, not the first.

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

We have defined a function, mixedNumbers, which we are passing an array as an argument. Modify the function by using push() and unshift() to add ‘I’, 2, ‘three’ to the beginning of the array and 7, ‘VIII’, 9 to the end so that the returned array contains representations of the numbers 1-9 in order.

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

What does the .splice() method allows us to do? How many parameter can it take?

A

To remove any number of consecutive elements from anywhere in an array.

Up to 3 parameters.

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

Explain the 2 first parameters of .splice().

A

The first parameter represents the index on the array from which to begin removing elements, while the second parameter indicates the number of elements to delete.

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

Besides cutting out elements from an array, what other thing does .splice() do?

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

What’s the third parameter of .splice() for?

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

What is the .slice() method?

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

What is the spread operator?

A

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

We have defined a function, copyMachine which takes arr (an array) and num (a number) as arguments. The function is supposed to return a new array made up of num copies of arr. We have done most of the work for you, but it doesn’t work quite right yet. Modify the function using spread syntax so that it works correctly (hint: another method we have already covered might come in handy here!).

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

What will be the output of the code below?

A

thatArray would have the value [‘basil’, ‘cilantro’, ‘sage’, ‘rosemary’, ‘parsley’, ‘thyme’, ‘coriander’].

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

What does the .indexOf() method do?

A

It takes an element as a parameter, and when called, it returns the position, or index, of that element, or -1 if the element does not exist on the array.

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

Explain the code below.

A

Using a for loop, this function iterates through and accesses each element of the array, and subjects it to a simple test that we have created. In this way, we have easily and programmatically determined which data items are greater than 10, and returned a new array, [12, 14, 80], containing those items.

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

We have defined a function, filteredArray, which takes arr, a nested array, and elem as arguments, and returns a new array. elem represents an element that may or may not be present on one or more of the arrays nested within arr. Modify the function, using a for loop, to return a filtered version of the passed array such that any array nested within arr containing elem has been removed.

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

How many levels can an array have?

A

Virtually infinite levels.

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

Here we’ve defined an object userActivity, which includes another object nested within it. Set the value of the online key to 45.

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

What’s the output of the below code?

A

This code will evaluate the value stored in the selectedFood variable and return the value of that key in the foods object, or undefined if it is not present

17
Q

Why is bracket notation useful?

A

Because sometimes object properties are not known before runtime or we need to access them in a more dynamic way.

18
Q

What will be the output of the code below?

A

If the object has the property, both will be true; otherwise, both will be false.

19
Q

Finish writing the function so that it returns true if the object passed to it contains all four names, Alan, Jeff, Sarah and Ryan and returns false otherwise.

A
20
Q

What is a “for…in” statement?

A

A JavaScrip piece of syntax that allows us to iterate through all the keys in an object.

21
Q

What should be noted about the position of keys in objects?

A

Objects do not maintain an ordering to stored keys like arrays do; thus a key’s position on an object, or the relative order in which it appears, is irrelevant when referencing or accessing that key.

22
Q

We’ve defined a function countOnline which accepts one argument (a users object). Use a for…in statement within this function to loop through the users object passed into the function and return the number of users whose online property is set to true. An example of a users object which could be passed to countOnline is shown below. Each user will have an online property with either a true or false value.

A
23
Q

What does the Object.keys() method do?

A

Takes an object as the argument and returns an array of strings representing each property in the object. Again, there will be no specific order to the entries in the array.