Skip to content

Instantly share code, notes, and snippets.

View Angelfire's full-sized avatar
👾
Hardcore JavaScript Developer

Andrés Bedoya (SrHart) Angelfire

👾
Hardcore JavaScript Developer
View GitHub Profile
@Angelfire
Angelfire / deepRetrieval.js
Created January 11, 2023 01:59
Deep Retrieval
// {
// prop: {
// prop: {
// prop: 3
// }
// }
// }
// {
// prop: 3
@Angelfire
Angelfire / LinkedList.js
Last active January 11, 2023 01:40
Linked List
class LinkedList {
constructor() {
this.head = null
}
addFirst(node) {
if (this.head === null) {
this.head = node
} else {
node.next = this.head
const Stack = require('./Stack');
class OperationManager {
constructor() {
this.operations = new Stack()
this.undos = new Stack()
}
addOperation(operation) {
this.operations.push(operation)
@Angelfire
Angelfire / group.js
Last active March 2, 2024 14:28
Grouping With Reduce
// 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:
// {
@Angelfire
Angelfire / removeDuplicates.js
Last active January 10, 2023 20:14
Remove Duplicates
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)
}
@Angelfire
Angelfire / largest.js
Last active January 10, 2023 17:49
Largest
// 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
});
}
@Angelfire
Angelfire / sortByMonth.js
Last active January 10, 2023 16:59
Sort By Months
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) {
@Angelfire
Angelfire / sortStudents.js
Created January 10, 2023 16:48
Sorting by Multiple Properties
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) {
@Angelfire
Angelfire / Circle.js
Last active January 10, 2023 15:15
Sharing Functionality
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.
@Angelfire
Angelfire / Celebrity.js
Created January 10, 2023 14:23
Unbound function
/* 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;