L2: Sorting, Nested Data Strucs, Flashcards

1
Q

Describe the sorting algorithm of .sort()

A

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)

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

What makes a first class value/object/function? (3 things)

A

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.

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

What is a function object called when we pass it as an agument into another function?

A

Callback function
or
callbacks

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

What is a higher order function?

A

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.

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

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;
});
});
});

A

[ undefined, undefined ]

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

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);
});

A

// => [ { c: ‘cat’, d: ‘dog’ } ]

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

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;
}
});
});

A

// => [ [ 27 ], [ ‘apple’ ] ]

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

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);
}
});
});

A

// => [ [ [ 2, 3 ], [ 4, 5 ] ], [ 6, 7 ] ]

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

Sort these in descending order
let arr = [‘10’, ‘11’, ‘9’, ‘7’, ‘8’]; (of numbers, not letters)

A

if b < a return -1
else as you would expect,
but did you remember to convert the strings to numbers! No implicit coersion!

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

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’]];

A

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’ ] ]

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

Create a sorting algorithm from scratch for letters

A

with numbers you can do a - b
for letters you can go a < b return -1

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

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’ },
};

A

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();
}
});

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

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] },
];

A

arr.filter(obj => {
return Object.values(obj).every(subArr => {
return subArr.every(num => num % 2 === 0);
});
});

// => [ { e: [ 8 ], f: [ 6, 10 ] } ]

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