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
/* | |
"Sometimes (when I nest them (my parentheticals) too much (like this (and this))) they get confusing." | |
Write a function that, given a sentence like the one above, along with the position of an opening parenthesis, finds the corresponding closing parenthesis. | |
Example: if the example string above is input with the number 10 (position of the first parenthesis), the output should be 79 (position of the last parenthesis). | |
*/ | |
function findClosingParenthesis(openingParenthesis, string) { | |
// use stack to track matching parenthesis |
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
function mergeSort(array) { | |
if (array.length < 2) return array; | |
// split the input array in half | |
const midPoint = Math.floor(array.length / 2); | |
const left = array.slice(0, midPoint); | |
const right = array.slice(midPoint); | |
// recursively call mergeSort on halved input | |
const sortedLeft = mergeSort(left); |
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
const middlewareA = store => { | |
return next => { | |
console.log("middlewareA next: ", next); | |
return action => { | |
console.log("running middlewareA"); | |
return next(action); | |
}; | |
}; | |
}; |
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
const combineReducers = reducers => { | |
return (state = {}, action) => { | |
// Reduce all the keys for reducers from `todos` and `visibilityFilter` | |
return Object.keys(reducers).reduce( | |
(nextState, key) => { | |
// Call the corresponding reducer function for a given key | |
nextState[key] = reducers[key] ( | |
state[key], | |
action |
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
const createStore = (reducer) => { | |
let state; | |
let listeners = []; | |
const getState = () => state; | |
const dispatch = (action) => { | |
state = reducer(state, action); | |
listeners.forEach(listener => listener()); | |
}; |
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
const delayedPromise = (delay = 1000, value = "foo", shouldReject) => { | |
return new Promise((resolve, reject) => { | |
const action = shouldReject ? reject : resolve; | |
setTimeout(() => action(value), delay); | |
}); | |
}; | |
delayedPromise().then(value => console.log(value)); | |
// expected output: "foo"); |