Skip to content

Instantly share code, notes, and snippets.

View shawn-dsz's full-sized avatar
🎯
Solving Problems beautifully

Shawn D'Souza shawn-dsz

🎯
Solving Problems beautifully
View GitHub Profile
@shawn-dsz
shawn-dsz / memo-.js
Created August 22, 2023 08:55
memo w/ lodash
import _ from 'lodash';
function swatch(color) {
console.log(`Color is ${color}`);
return `Color is ${color}`
}
const memoizedSwatch = _.memoize(swatch);
memoizedSwatch("blue");
memoizedSwatch("red");
@shawn-dsz
shawn-dsz / containDuplicate
Last active July 28, 2022 06:45
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
const expect = (actual, expected) => {
if (actual !== expected) {
console.log(`Expected ${actual} to equal ${expected}`);
}
};
var containsDuplicate = function (nums) {
var map = new Map();
for (const elm of nums) {
@shawn-dsz
shawn-dsz / memoizefunc.js
Created December 19, 2021 10:13
create memoize function
const createHash = (item) => JSON.stringify(item);
const memoize = (original) => {
const cache = {};
return (...args) => {
const hash = createHash(args);
if (cache.hasOwnProperty(hash)) {
return cache[hash];
}
cache[hash] = original(...args);
@shawn-dsz
shawn-dsz / index.ts
Created December 19, 2021 10:06
flatten nested object with array recursive with reduce manually
const array = [
{
name: 'salvador',
age: 99,
gender: 'male',
children: [
{
name: 'felix',
age: 67,
gender: 'male',
@shawn-dsz
shawn-dsz / toggle-selectd-findById.ts
Created December 18, 2021 10:07
toggle-selectd-findById
const toppings = [
{
id: 1,
subOptions: [
{ id: 2, subOptions: [{ id: 3, checked: false }], checked: false },
],
checked: false,
},
{ id: 2, subOptions: [], checked: false },
{ id: 3, subOptions: [{ id: 5, checked: false }], checked: false },
@shawn-dsz
shawn-dsz / recursively add prop to array objects.ts
Last active December 18, 2021 09:35
recursively add prop to array objects
// interface Topping {
// // name: string;
// id: number;
// subOptions?: Topping[];
// checked?: boolean;
// }
const toppings = [
{
id: 1,
@shawn-dsz
shawn-dsz / smartSquare.ts
Created December 18, 2021 04:50
use memo as lookup for n2 problem
const square = (n: number) => {
let res = 0;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
i;
j;
res += 1;
}
}
return res;
@shawn-dsz
shawn-dsz / getPetWithGoldnames.ts
Last active December 18, 2021 05:13
query nested object keys
const animals = [
{
id: 1,
type: 'cat',
pets: [
{ id: 1, name: 'Toby', breed: 'Tabby', cute: true },
{ id: 2, name: 'Golden Girl', breed: 'Russian Blue', cute: false },
],
},
{
@shawn-dsz
shawn-dsz / getValues.ts
Created December 18, 2021 02:37
get id recursively
const myObj = {
id: 1,
anyProp: [{
id: 2,
thing: { a: 1, id: 10 },
children: [{ id: 3 }]
}, {
id: 4,
children: [{
id: 5,
@shawn-dsz
shawn-dsz / findAllByKey.ts
Created December 18, 2021 01:07
recursively find objects by key
const data = [
{
title: 'some title',
channel_id: '123we',
options: [
{
channel_id: 'abc',
image: 'http://asdasd.com/all-inclusive-block-img.jpg',
title: 'All-Inclusive',
options: [