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.


What’s the output of the below code?

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
Why is bracket notation useful?
Because sometimes object properties are not known before runtime or we need to access them in a more dynamic way.
What will be the output of the code below?

If the object has the property, both will be true; otherwise, both will be false.
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.

What is a “for…in” statement?
A JavaScrip piece of syntax that allows us to iterate through all the keys in an object.

What should be noted about the position of keys in objects?
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.
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.

What does the Object.keys() method do?
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.