Basic data structures Flashcards
What does “zero-indexed” mean for an array?
The first element of an array is actually at the zeroth position, not the first.
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.
What does the .splice() method allows us to do? How many parameter can it take?
To remove any number of consecutive elements from anywhere in an array.
Up to 3 parameters.
Explain the 2 first parameters of .splice().
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.
Besides cutting out elements from an array, what other thing does .splice() do?
What’s the third parameter of .splice() for?
What is the .slice() method?
What is the spread operator?
…
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!).
What will be the output of the code below?
thatArray would have the value [‘basil’, ‘cilantro’, ‘sage’, ‘rosemary’, ‘parsley’, ‘thyme’, ‘coriander’].
What does the .indexOf() method do?
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.
Explain the code below.
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.
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.
How many levels can an array have?
Virtually infinite levels.
Here we’ve defined an object userActivity, which includes another object nested within it. Set the value of the online key to 45.