This file contains 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
/** | |
* @param {any} o1 | |
* @param {any} o2 | |
* @return {boolean} | |
*/ | |
const arrayEqual = (arr1, arr2) => { | |
const array1Length = arr1.length | |
const array2Length = arr2.length |
This file contains 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 createCounter = function (init) { | |
// Keep in memory by initiating a count | |
let initialValue = init | |
let count = init | |
// Return methods for the function | |
return { | |
increment: () => ++count, | |
decrement: () => --count, | |
reset: () => count = initialValue |
This file contains 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 memoize(fn) { | |
let cache = new Map(); | |
return function (...args) { | |
let key = JSON.stringify(args); | |
if (cache.has(key)) { | |
return cache.get(key); | |
} | |
This file contains 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
/** | |
* @param {Function[]} functions | |
* @return {Function} | |
*/ | |
var compose = function (functions) { | |
// This returns a function, | |
return function (x) { | |
// Initialize the output as the x parameter |
This file contains 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
/** | |
* @param {number} millis | |
*/ | |
async function sleep(millis) { | |
// setTimeout cannot be used conjunction with async await | |
// You have to return a new Promise, that passes the resolve fn to setTimeout after milliseconds | |
return new Promise(resolve => setTimeout(resolve, millis)) | |
} |
This file contains 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
/** | |
* @param {any[]} arr | |
* @param {number} depth | |
* @return {any[]} | |
*/ | |
var flat = function (arr, n) { | |
// If base case is reached, only return the array. | |
// Reasoning is, you don't want to return a spread array | |
// ...ONLY IF n reaches 0. Otherwise, you want to continue | |
if (n === 0) { |
This file contains 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 Game | |
# Manages the state of the game | |
# Can change turns, ask questions to players, check answers | |
def initialize(players, max_points) | |
@index = 0 | |
@players = players | |
@max_points = max_points | |
@is_start = false | |
end |
This file contains 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 Person | |
attr_accessor :name | |
def initialize(name) | |
@name = name | |
end | |
end | |
p = Person.new("L. Ron") | |
puts p.name |
This file contains 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
def benchmark | |
# Mark current time | |
start_time = Time.now() | |
# Run long callback / yield command | |
yield | |
# Mark end time | |
end_time = Time.now() | |
# Calculate total time using marked times | |
total_time = end_time - start_time |
This file contains 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 Vampire { | |
constructor(name, yearConverted) { | |
this.name = name; | |
this.yearConverted = yearConverted; | |
this.offspring = []; | |
this.creator = null; | |
} | |
/** Simple tree methods **/ |
NewerOlder