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 factorial = trampoline(function _factorial(n, acc = 1) { | |
| if (n <= 1) return acc; | |
| return () => _factorial(n - 1, n * acc); | |
| }); | |
| console.log(factorial(5)); |
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
| function trampoline(f) { | |
| return function trampolined(...args) { | |
| let result = f.bind(null, ...args); | |
| while (typeof result === 'function') result = result(); | |
| return result; | |
| }; | |
| } |
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
| function traverseDOM(tree) { | |
| if (isLeaf(tree)) tree.className = 'leaf'; | |
| else for (const leaf of tree.childNodes) traverseDOM(leaf); | |
| } |
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
| function recursiveFn(n) { | |
| if (n < 0) return true; | |
| recursiveFn(n - 1); | |
| } | |
| recursiveFn(100000); // RangeError: Maximum call stack size exceeded |
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
| function factorial(n) { | |
| if (n <= 1) return 1; | |
| return n * factorial(n - 1); | |
| } | |
| console.log(factorial(5)); // 120 |
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 Modal = ({children}) => <div className='Modal'> | |
| {children} | |
| </div>; |
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 App = () => { | |
| const [showModal, setShowModal] = useState(false); | |
| const handleKeyup = e => e.keyCode === 27 && setShowModal(false); | |
| const toggleModal = () => setShowModal(!showModal); | |
| useEffect(() => { | |
| if (showModal) window.addEventListener('keyup', handleKeyup); | |
| return () => window.removeEventListener('keyup', handleKeyup); | |
| }); | |
| return <div onClick={toggleModal} className="App"> |
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
| .Modal { | |
| /* fixed position will always show modal in the top left corner */ | |
| position: fixed; | |
| top: 0; | |
| left: 0; | |
| /* z-index to show modal even if it is used before some other elements */ | |
| z-index: 5; | |
| /* make modal fill whole window */ | |
| width: 100vw; | |
| height: 100vh; |
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
| for (const el of array) { | |
| // do something here | |
| } |
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.prototype.forEach(el => { | |
| // do something here | |
| }); |