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
| // a simple pure function to get a value adding 10 | |
| const add = (n) => (n + 10); | |
| console.log('Simple call', add(3)); | |
| // a simple memoize function that takes in a function | |
| // and returns a memoized function | |
| const memoize = (fn) => { | |
| let cache = {}; | |
| return (...args) => { | |
| let n = args[0]; // just taking one argument here | |
| if (n in cache) { |
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
| // Time complexity: O(n^2) | |
| function findTriplets(arr, n) { | |
| arr.sort(); | |
| var l = arr.length; | |
| for (var i = 0; i < l; i++) { | |
| var j = i + 1, | |
| k = l - 1; | |
| while (j < k) { | |
| if (arr[i] + arr[j] + arr[k] < n) { | |
| j++; |