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
// store.js | |
import { configureStore } from "@reduxjs/toolkit"; | |
import scoreBoardSlice from '../ScoreBoard/scoreBoardSlice'; // ignore | |
import { loadState, saveState } from './localStorage'; // <-- our utility of localstorage | |
import { debounce } from './index'; // <-- our utility to prevent writing too often. | |
const persistedState = loadState(); | |
const store = configureStore({ | |
reducer: scoreBoardSlice.reducer, | |
preloadedState: persistedState, // <--- where we pre-load data we saved to local storeage |
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
//localStorage.js | |
export const loadState = () => { | |
try { | |
const serializedState = localStorage.getItem('state'); | |
if (serializedState === null) { | |
return undefined; | |
} | |
return JSON.parse(serializedState); | |
} catch (err) { | |
return undefined; |
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
/** | |
This is for educational purpose only and for fun. | |
*/ | |
class customPromise { | |
constructor(functionToResolve) { | |
this.status = "pending"; | |
this.onFulfilledCallbacks = []; | |
this.onRejectedCallbacks = []; |
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
let cache = {}; | |
var yourself = { | |
fibonacci : function(n) { | |
if (n === 0) { | |
return 0; | |
} else if (n === 1) { | |
return 1; | |
} else { | |
let r = cache[n] = cache[n] ? cache[n] : this.fibonacci(n - 1) + |
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
/** | |
Examples | |
"the-stealth-warrior" gets converted to "theStealthWarrior" | |
"The_Stealth_Warrior" gets converted to "TheStealthWarrior" | |
**/ | |
function toCamelCase(str){ | |
return str.replace(/[-_][a-zA-Z]/g, (i) => i.replace(/[-_]/g,'').toUpperCase()); | |
} |
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
/** | |
solution('XXI'); // should return 21 | |
**/ | |
const helper = { | |
'I': 1, | |
'V': 5, | |
'X': 10, | |
'L': 50, | |
'C': 100, |
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
/** | |
list([ {name: 'Bart'}, {name: 'Lisa'}, {name: 'Maggie'} ]) | |
// returns 'Bart, Lisa & Maggie' | |
**/ | |
function list(names){ | |
let formatNames = ''; | |
names.map((a,b) => (b < names.length-2) ? formatNames += a.name + ', ' : ((b === names.length-2) ? (formatNames += a.name + ' & ') : (formatNames += a.name))) | |
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
/** | |
array = [[1,2,3], | |
[4,5,6], | |
[7,8,9]] | |
snail(array) #=> [1,2,3,6,9,8,7,4,5] | |
**/ | |
snail = function(array) { | |
let results = [], | |
botArray = []; |
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
/** | |
var result = solution("apples, pears # and bananas\ngrapes\nbananas !apples", ["#", "!"]) | |
// result should == "apples, pears\ngrapes\nbananas" | |
**/ | |
function solution(input, markers) { | |
return input.replace(new RegExp('(\\s?['+markers.join('?') + '?' + '\-?]\\s?)(.*)','gm'),''); | |
} |