JavaScript Collections: Arrays and Objects Flashcards
What happens when you provide negative arguments to the slice method?
When given negative numbers as arguments, slice method treats them as the ‘string length + index’.
e.g. ‘abcdefghi’.slice(-4, -2)
Index -4 is equal to 9 + (-4) = 5
Index -2 is equal to 9 + (-2) = 7
> ‘abcdefghi’.slice(5, 7) // => ‘fg’
What are the 2 key differences between slice and substring methods?
- When the start index > end index, substring will implicitly swap the arguments while slice will return an empty string.
- When either argument is negative, substring will treat them as 0 and return the whole string.
Note: Slice is preferred since the behavior is more predictable when it comes to edge cases.
Will slicing a copy of the array and modifying its values mutate the original array? Please explain.
Slicing an array will create a shallow copy. If the original array/object contains ONLY primitive values, then the original array/object will not be affected when the copied array/object is mutated.
e.g. > let arr = ['a', 'b', 'c', 'd'] > let arrCopy = arr.slice() > arrCopy.push('e') > arr // => [ 'a', 'b', 'c', 'd' ] > arrCopy // => [ 'a', 'b', 'c', 'd', 'e' ]
However, if the array or object contains nested objects or arrays, changing the data nested in the original will mutate the copied array/object.
e.g. > let nestedArr = [ 'a', 'b', [1, 2] ] > let nestedArrCopy = nestedArr.slice() > nestedArrCopy[2].push(3) > arr // => [ 'a', 'b', [1, 2, 3] ] > arrCopy // => [ 'a', 'b', [1, 2, 3] ]
What are the three types of collections?
Strings, arrays, and objects are all collection data structures made up of individual elements. Arrays are objects.
Strings use an integer-based index that represents each character in the string.
Similar to strings, arrays are a list of elements that are ordered by index. However, each element can be any value, not just characters.
Objects are another collection data structure that uses key-value pairs instead of an integer-based index. The key (aka property) is a string and the value can be any value.
What happens when an object has the same key/property name?
When initializing an object, keys/properties must be unique. If there are duplicates, the first value will be overwritten by the most recent value.
Values, however, can be duplicated.
What will an out-of-bounds index return? A negative index?
Out of bounds and negative indexes of a string or an array will return undefined.
How do you differentiate between a non-existent property and a property with undefined as a value?
Two ways:
object.hasOwnProperty(‘key’); // => true/false
OR
Object.keys(object).includes(‘key’) // => true/false
Since arrays are objects, what happens when you add properties to arrays?
You can add properties to arrays, but it will not affect its length property are recognized by array methods such as forEach, map, filter, reduce.
e.g.
> let arr = [‘a’, ‘b’, ‘c’];
> arr[‘boo’] = ‘hoo’;
> arr[-1] = 374;
> arr; // => [ ‘a’, ‘b’, ‘c’, boo: ‘hoo’, ‘-1’: 374 ]
> arr.length; // => 3 (not 5!)
arr.forEach(element => console.log(element)); // => prints: foo, bar, qux
How do you convert a string to an array and vice versa? How about an object to an array?
string –> array
string.split()
array –> string
string.join()
object –> array
Object.entries(object)
What are some useful string methods?
.concat() .trim() .includes() .split() .toUpperCase() and .toLowerCase() .charCodeAt (using unicode) .repeat()
What is a guard clause in loops?
A guard clause is a conditional statement that allows the body of a loop or function to skip values it doesn’t need to handle.
Guard clauses always include a continue, break, or return statement in the body of the ‘if’ statement. It’s common practice to use the single-line version of the if statement with guard clauses
e.g.
> let numbers = [ 1, 4, 3, 7, 6, 5, 2, 1 ];
> for (let index = 0; index < numbers.length; index += 1) {
> if (numbers[index] % 2 === 1) continue;
> let square = numbers[index] * numbers[index];
> console.log(square);
> }
// In this case, we don’t need the main body of the loop to process odd numbers, so a guard clause is used at the top of the loop. The continue statement is used to terminate the current iteration of the loop and process the next value.
Note: The continue statement does not restart the loop, it ends the current iteration and starts the next.
What are the four primary elements in looping?
- A looping construct (e.g. for or while)
- A counter (control varaible)
- A way to retrieve a current value
- A way to exit the loop
Describe the PEDAC process.
P: Understand the problem
(1) Establish explicit rules / boundaries of the problem with the available information
(2) Check the test cases if there are any
(3) Clarify any part of the problem that is unclear. e.g. ask about base cases or assumptions (implicit)
E: Examples / Test cases
D: Data Structure (often paired with algorithm)
A: Algorithm (often paired with data structure)
C: Implement code
What is the difference between selection and transformation of collections?
Selection is picking some elements out of a collection depending on a criteria while transformation refers to iterating through or manipulating every element in the collection.
When performing a transformation, it is important to note whether the original collection is mutated or if a new collection is returned.
Note: It is still considered a transformation if some elements are left unchanged but the whole length of the array is iterated over and returned.
What are some useful array methods?
JavaScript arrays supply more than 30 instance and static methods
.forEach() //Iteration, returns undefined
.filter() // Selection/Filtering, returns new array
.map() // Transformation, returns new array
.reduce()
.some() // returns true if any ONE element is true
.every() // returns true if ALL elements are true
.find() // returns first element that is true
.findIndex() returns first index that is true
.includes() // doesn’t take callback, but an argument
.reverse()