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
//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 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 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 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 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 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 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 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'),''); | |
} |
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
/** | |
Example: | |
Calculator().evaluate("2 / 2 + 3 * 4 - 6") # => 7 | |
**/ | |
const Calculator = function() { | |
this.resolveMultiDivision = mathString => { | |
var resolvedTop = mathString.replace(new RegExp(/(\d*\.?\d*)([\/\*])(\d*\.?\d*)/), (match, group1,group2,group3) => { | |
return group2 === '*' ? group1 * group3 : group1 / group3; |