L2: Sorting, Nested Data Strucs, Flashcards
Describe the sorting algorithm of .sort()
It wants to sort them such that the left most array items are the smallest.
A is the element, b is the next element.
So if A - B is negative, A goes first (because it’s smaller)
What makes a first class value/object/function? (3 things)
They can be assigned to a variable or an element of a data structure (such as an array or object).
They can be passed as an argument to a function.
They can be returned as the return value of a function.
What is a function object called when we pass it as an agument into another function?
Callback function
or
callbacks
What is a higher order function?
Does one or more of:
takes one or more functions as arguments
returns a function as its result
Array.prototype.forEach is a higher-order function - we can pass another function (the callback) to forEach as an argument.
What is returned from this:
[[[1], [2], [3], [4]], [[‘a’], [‘b’], [‘c’]]].map(element1 => {
return element1.forEach(element2 => {
return element2.filter(element3 => {
return element3.length > 0;
});
});
});
[ undefined, undefined ]
What is returned from this:
[{ a: ‘ant’, b: ‘elephant’ }, { c: ‘cat’, d: ‘dog’ }].filter(object => {
return Object.keys(object).every(key => object[key][0] === key);
});
// => [ { c: ‘cat’, d: ‘dog’ } ]
What is returned from this:
[[8, 13, 27], [‘apple’, ‘banana’, ‘cantaloupe’]].map(arr => {
return arr.filter(item => {
if (typeof item === ‘number’) {
return item > 13;
} else {
return item.length < 6;
}
});
});
// => [ [ 27 ], [ ‘apple’ ] ]
What does this return:
[[[1, 2], [3, 4]], [5, 6]].map(arr => {
return arr.map(elem => {
if (typeof elem === ‘number’) { // it’s a number
return elem + 1;
} else { // it’s an array
return elem.map(number => number + 1);
}
});
});
// => [ [ [ 2, 3 ], [ 4, 5 ] ], [ 6, 7 ] ]
Sort these in descending order
let arr = [‘10’, ‘11’, ‘9’, ‘7’, ‘8’]; (of numbers, not letters)
if b < a return -1
else as you would expect,
but did you remember to convert the strings to numbers! No implicit coersion!
Given the following data structure, return a new array with the same structure, but with the values in each subarray ordered – alphabetically or numerically as appropriate – in ascending order.
let arr = [[‘b’, ‘c’, ‘a’], [2, 11, -3], [‘blue’, ‘black’, ‘green’]];
arr.map(subArr => {
if (typeof subArr[0] === ‘string’) {
// we have an array of strings
return subArr.slice().sort();
} else {
// we have an array of numbers
return subArr.slice().sort((a, b) => a - b);
}
});
// [ [ ‘a’, ‘b’, ‘c’ ], [ -3, 2, 11 ], [ ‘black’, ‘blue’, ‘green’ ] ]
Create a sorting algorithm from scratch for letters
with numbers you can do a - b
for letters you can go a < b return -1
Given the following data structure write some code to return an array containing the colors of the fruits and the sizes of the vegetables. The sizes should be uppercase, and the colors should be capitalized.
Can be a nested array.
don’t use for in.
// if fruit return return colour capitalized, if veg, return size uppercase
let obj = {
grape: { type: ‘fruit’, colors: [‘red’, ‘green’], size: ‘small’ },
carrot: { type: ‘vegetable’, colors: [‘orange’], size: ‘medium’ },
apple: { type: ‘fruit’, colors: [‘red’, ‘green’], size: ‘medium’ },
apricot: { type: ‘fruit’, colors: [‘orange’], size: ‘medium’ },
marrow: { type: ‘vegetable’, colors: [‘green’], size: ‘large’ },
};
let capitalize = word => word[0].toUpperCase() + word.slice(1);
Object.values(obj).map(attributes => {
if (attributes[‘type’] === ‘fruit’) {
return attributes[‘colors’].map(char => capitalize(char));
} else {
return attributes[‘size’].toUpperCase();
}
});
Given the following data structure, write some code to return an array which contains only the objects where all the numbers are even.
(don’t use flat)
let arr = [
{ a: [1, 2, 3] },
{ b: [2, 4, 6], c: [3, 6], d: [4] },
{ e: [8], f: [6, 10] },
];
arr.filter(obj => {
return Object.values(obj).every(subArr => {
return subArr.every(num => num % 2 === 0);
});
});
// => [ { e: [ 8 ], f: [ 6, 10 ] } ]