Skip to content

Instantly share code, notes, and snippets.

rotateAndCache = function(image,angle) {
var offscreenCanvas = document.createElement('canvas');
var offscreenCtx = offscreenCanvas.getContext('2d');
var size = Math.max(image.width, image.height);
offscreenCanvas.width = size;
offscreenCanvas.height = size;
offscreenCtx.translate(size/2, size/2);
offscreenCtx.rotate(angle + Math.PI/2);
const assert = function (condition, message) {
try {
console.assert.apply(console, arguments);
if (typeof message === 'string' && condition) {
console.log('✔ ' + message);
}
} catch(error) {
assert.exitCode = 1;
console.error('✖ ' + error);
}
var LFUCache = function(capacity) {
this.cache = {};
this.capacity = capacity;
this.population = 0;
};
LFUCache.prototype.get = function(key) {
if(this.LRUkey && key === this.LFUkey) {
this.cache[key].use += 1;
//check if any others have an equal amount of uses
const productExceptSelf = (nums) => nums.map((num,i) => nums.reduce((acc,number,j) => (i!==j ? acc *= number : acc), 1));
function TreeNode(val) {
this.val = val;
this.left = this.right = null;
}
const kthSmallest = function(root, k, smallestArray=[]) {
if(smallestArray.length < k) {
smallestArray.push(root.val);
smallestArray.sort((a, b) => a - b);
} else if(smallestArray.find(value => value > root.val)) {
function fetchSomeResources() {
return dispatch => {
// Async action is starting...
dispatch({type: FETCH_RESOURCES});
someHttpClient.get('/resources')
// Async action succeeded...
.then(res => {
dispatch({type: FETCH_RESOURCES_SUCCESS, data: res.body});
const hasPathToSum = function(node, targetSum) {
let targetSumCopy = targetSum - node.value;
if(targetSumCopy === 0 && node.children.length === 0) {
return true;
} else if(targetSumCopy < 0 || node.children.length === 0 ){
return false;
} else {
//target sum > 0 and there are children
var find = node.children.find(child => hasPathToSum(child, targetSumCopy));
if(find) {
var cashAmount = function(amount) {
this.amount = amount * 100;
this.reduceBy = function(amountToModulo) {
var divideCount = 0;
console.log('amount: ', this.amount);
while(this.amount >= amountToModulo) {
this.amount -= amountToModulo;
divideCount++;
}
return divideCount;
@OneCent01
OneCent01 / color object
Last active January 3, 2022 07:28
Every color contained in an object with its corresponding hexa value for O(1) time lookup, find function below object
var colors = {
ALICEBLUE: '#F0F8FF',
ANTIQUEWHITE: '#FAEBD7',
AQUA: '#00FFFF',
AQUAMARINE: '#7FFFD4',
AZURE: '#F0FFFF',
BEIGE: '#F5F5DC',
BISQUE: '#FFE4C4',
BLACK: '#000000',
BLANCHEDALMOND: '#FFEBCD',
var Board = function(n) {
this.board = [];
for(var i = 0; i < n; i++) {
var row = [];
for(var j = 0; j < n; j++) {
row.push(0);
}
this.board.push(row);
}
};