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 withCache = (fn, limit) => { | |
const cache = {}; | |
const cacheQueue = new Array(limit); | |
return (params) => { | |
// console.log('cache', cacheQueue, cache) | |
const key = fn.name + '$$' + JSON.stringify(params); | |
if (cache.hasOwnProperty(key)) { |
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
import React, { useRef } from "react"; | |
import ReactDOM from "react-dom"; | |
import "./styles.css"; | |
const API_TOKEN = "MfwZLqWeh1YPrvrlULOqgzZfOlRkIR7r"; | |
const BASE_URL = "https://api.giphy.com/v1/gifs/search?api_key="; | |
const SEARCH_URL = BASE_URL + API_TOKEN + "&q="; | |
/* |
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
// Callback to Promise | |
const printMessage = (param, callback) => { | |
console.log('Executing...', param); | |
callback('err', {param: param, done: 'yes'}); | |
} | |
const proisify = fn => params => new Promise((resolve, reject) => { | |
fn(params, (err, data) => { | |
if (err) reject(err); |
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
// Implement a runOnlyOnce function | |
const runOnlyOnce = fn => { | |
let flag = false; | |
let result = null; | |
return (...args ) => { | |
if (flag) return result; | |
result = fn(args); | |
flag = true; |
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 str1 = 'my--name---is-vipin-'; // myNameIsVipin | |
const str2 = '---this-is-awesome----day---too'; // ThisIsAwesomeDayToo | |
const snakeToCamel = (str) => { | |
const strArr = str.split(/-+/); | |
const flag = str.startsWith('-') | |
return strArr.map((st, i) => { | |
let firstChar = st.charAt(0); |
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
// Possible sum without neighbours | |
const arr = [4,1,5,7,2,3,8,6]; | |
const arr2 = [1,1,1,4,1,2,4] | |
function maxSum(arr) { | |
if (!arr.length) return 0; | |
if (arr.length === 1) { | |
return arr[0]; |
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
// Implement lodash cloneDeep | |
const a = { | |
val: 1, | |
prop: { | |
val2: 0, | |
val3: { name: 'a', condition: false, myArr: [1,2,3,4]} | |
} | |
}; |
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 obj = {asdf: "jkl", num: 2, bool: false, subObj: {a: 1, b: true}, arr: [1, 'abc', true]}; | |
const str = JSON.stringify(obj); | |
console.log(str); | |
const myStringify = (value) => { | |
let str = ''; | |
if (typeof value === 'string') { | |
str += '"' + value + '"'; | |
} else if (typeof value === 'number') { |
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 S = "timetopractice"; | |
const P = "toc"; | |
const P_arr = P.split(''); | |
const matchedSubStr = []; | |
let i = 0; | |
let win = P.length; | |
while (i + win <= S.length) { | |
const subStr = S.substring(i, i+win); |
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 romanMap = { | |
1: 'I', | |
2: 'II', | |
3: 'III', | |
4: 'IV', | |
5: 'V', | |
6: 'VI', | |
7: 'VII', | |
8: 'VIII', | |
9: 'IX', |