This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// { | |
// prop: { | |
// prop: { | |
// prop: 3 | |
// } | |
// } | |
// } | |
// { | |
// prop: 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LinkedList { | |
constructor() { | |
this.head = null | |
} | |
addFirst(node) { | |
if (this.head === null) { | |
this.head = node | |
} else { | |
node.next = this.head |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Stack = require('./Stack'); | |
class OperationManager { | |
constructor() { | |
this.operations = new Stack() | |
this.undos = new Stack() | |
} | |
addOperation(operation) { | |
this.operations.push(operation) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Given an array of fruits in this format: | |
// [ | |
// { food: 'apple', type: 'fruit' }, | |
// { food: 'orange', type: 'fruit' }, | |
// { food: 'carrot', type: 'vegetable' } | |
// ] | |
// Let's turn it into an object with this format: | |
// { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function removeDuplicates(arr) { | |
return arr.filter((el, idx) => arr.indexOf(el) === idx) | |
} | |
function removeDuplicates(numbers) { | |
return numbers.reduce((accumulator, currentValue) => { | |
if(accumulator.indexOf(currentValue) === -1) { | |
accumulator.push(currentValue) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// By default, the reduce function will use the first value in the array as the accumulator if an accumulator is not provided. | |
// numbers is an array full of numbers | |
// let's find the largest and return it | |
// i.e. [2,3,5,1,4] => 5 | |
function largest(numbers) { | |
return numbers.reduce((accumulator, currentValue) => { | |
return accumulator > currentValue ? accumulator : currentValue | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MONTHS = [ | |
'JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', | |
'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC' | |
]; | |
const events = [{ event: 'dance', month: 'MAR' }, | |
{ event: 'farmers market', month: 'JUN' }, | |
{ event: 'parade', month: 'JAN' }] | |
function sortByMonth(events) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const students = [ | |
{ id: 1, graduated: true, grade: 86 }, | |
{ id: 2, graduated: false, grade: 96 }, | |
{ id: 3, graduated: false, grade: 78 }, | |
{ id: 4, graduated: true, grade: 96 }, | |
]; | |
function sortStudents(students) { | |
return students.sort((a, b) => { | |
if (a.graduated !== b.graduated) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Shape = require('./Shape'); | |
function Circle(x, y, radius) { | |
Shape.call(this, x, y); | |
// store radius on this | |
this.radius = radius | |
} | |
// Object.create is used to link our prototypes within the prototype chain. | |
// Now our Circle prototype inherits methods from the Shape Prototype! Any new circle will have a move method. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Within the function Celebrity, a method is used to fetch the celebrity's age. The second argument to fetchAge is a callback function. The callback function will receive age as an argument. | |
⚠️ Unfortunately, due to the function call-site, this will be re-defined to not refer to the celebrity. Running the tests without modifying the code will result in a TypeError. | |
Fix this.age to refer to the same this as the function Celebrity. | |
*/ | |
const fetchAge = require('./fetchAge'); | |
function Celebrity(name) { | |
this.name = name; |