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
| // Finds the index of a given element in a sorted array using the binary search algorithm. | |
| // | |
| // - Declare the left and right search boundaries, `l` and `r`, initialized to `0` and the `length` of the array respectively. | |
| // - Use a `while` loop to repeatedly narrow down the search subarray, using `Math.floor()` to cut it in half. | |
| // - Return the index of the element if found, otherwise return `-1`. | |
| // - **Note:** Does not account for duplicate values in the array. | |
| const binarySearch = (arr, item) => { |
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
| // Checks if a date is after another date. | |
| // Use the greater than operator (`>`) to check if the first date comes after the second one. | |
| const isAfterDate = (dateA, dateB) => dateA > dateB; | |
| isAfterDate(new Date(2010, 10, 21), new Date(2010, 10, 20)); // true |
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
| // Returns the elements that exist in both arrays, filtering duplicate values. | |
| // Create a `Set` from `b`, then use `Array.prototype.filter()` on `a` to only keep values contained in `b`. | |
| const intersection = (a, b) => { | |
| const s = new Set(b); | |
| return [...new Set(a)].filter(x => s.has(x)); | |
| }; |
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
| // Finds all unique values of an array, based on a provided comparator function, starting from the right. | |
| // - Use `Array.prototype.reduceRight()` and `Array.prototype.some()` to create an array containing only the last unique occurrence of each value, based on the comparator function, `fn`. | |
| // - The comparator function takes two arguments: the values of the two elements being compared. | |
| const uniqueElementsByRight = (arr, fn) => | |
| arr.reduceRight((acc, v) => { |
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
| // Checks if all the elements in `values` are included in `arr`. | |
| // - Use `Array.prototype.every()` and `Array.prototype.includes()` to check if all elements of `values` are included in `arr`. | |
| const includesAll = (arr, values) => values.every(v => arr.includes(v)); | |
| // includesAll([1, 2, 3, 4], [1, 4]); // true | |
| // includesAll([1, 2, 3, 4], [1, 5]); // false |
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
| // Interval: [start, end]. | |
| // Merges two overlapping intervals into one. | |
| function intervalsMerge(a, b) { | |
| return [Math.min(a[0], b[0]), Math.max(a[1], b[1])]; | |
| } | |
| const deepEqual = require('./deepEqual'); | |
| console.log(deepEqual(intervalsMerge([1, 2], [1, 4]), [1, 4])); | |
| console.log(deepEqual(intervalsMerge([1, 2], [0, 4]), [0, 4])); |
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 binToInt(binary) { | |
| let res = 0; | |
| for (let i = 0; i < binary.length; i++) { | |
| res = res * 2 + (+binary[i]); | |
| } | |
| return res; | |
| } | |
| // console.log(binToInt('0') === parseInt('0', 2) && parseInt('0', 2) === 0); |
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
| // Calculates the distance between two points in any number of dimensions. | |
| // - Use `Object.keys()` and `Array.prototype.map()` to map each coordinate to its difference between the two points. | |
| // - Use `Math.hypot()` to calculate the Euclidean distance between the two points. | |
| const euclideanDistance = (a, b) => Math.hypot(...Object.keys(a).map(k => b[k] - a[k])); | |
| euclideanDistance([1, 1], [2, 3]); // ~2.2361 |
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
| // Generates an array, containing the Fibonacci sequence, up until the nth term. | |
| // - Use `Array.from()` to create an empty array of the specific length, initializing the first two values (`0` and `1`). | |
| // - Use `Array.prototype.reduce()` and `Array.prototype.concat()` to add values into the array, using the sum of the last two values, except for the first two. | |
| const fibonacci = n => | |
| Array.from({ length: n }).reduce( | |
| (acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i), | |
| [] | |
| ); |
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
| // Encrypts or decrypts a given string using the Caesar cipher. | |
| // - Use the modulo (`%`) operator and the ternary operator (`?`) to calculate the correct encryption/decryption key. | |
| // - Use the spread operator (`...`) and `Array.prototype.map()` to iterate over the letters of the given string. | |
| // - Use `String.prototype.charCodeAt()` and `String.fromCharCode()` to convert each letter appropriately, ignoring special characters, spaces etc. | |
| // - Use `Array.prototype.join()` to combine all the letters into a string. | |
| // - Pass `true` to the last parameter, `decrypt`, to decrypt an encrypted string. | |
| const caesarCipher = (str, shift, decrypt = false) => { | |
| const s = decrypt ? (26 - shift) % 26 : shift; |