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
| // note that the routine would break for much of the unicode range | |
| const latinWithPunctuation = /^[a-zA-Z\s.,\/#!$%\^&\*;:{}=\-_`~()?"'’\-]*$/; | |
| const toggleCharCode = cc => | |
| (cc >= 97 && cc <= 122) | |
| ? cc - 32 | |
| : (cc >= 65 && cc <= 90) | |
| ? cc + 32 | |
| : cc; |
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 validatePizza = (layers, rules) => | |
| rules.find(([a, b]) => layers.indexOf(a) > layers.indexOf(b)) || 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
| function resolvePath(fs, path) { | |
| if (!(path in fs)) return null; | |
| const { [path]: nextPath, ...others } = fs; | |
| return nextPath ? resolvePath(others, nextPath) : path; | |
| } |
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 resolvePath = (fs, path, ...visited) => | |
| visited.includes(path) | |
| ? null | |
| : fs[path] | |
| ? resolvePath(fs, fs[path], path, ...visited) | |
| : path | |
| ; |
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 swap = (xs, n, i) => | |
| xs.fill( | |
| xs.splice(i+1, 1, n)[0], | |
| i, | |
| i+1 | |
| ) | |
| ; | |
| const chase = (xs, n, i) => | |
| i === xs.length - 1 |
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
| // thanks: https://stackoverflow.com/a/16353241 | |
| const isLeapYear = year => | |
| ( | |
| // divisible by 4 but not 100 | |
| (year % 4 == 0) && (year % 100 != 0) | |
| ) || ( | |
| // divisible by 400 | |
| year % 400 == 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
| const flippedy = str => | |
| str | |
| ?.split(" ") | |
| .map(word => [ | |
| word.match(/[aeiou]/gi)?.length || 0, | |
| word | |
| ]) | |
| .reduce((a, word, i) => | |
| 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
| const hungryBears = bears => | |
| bears | |
| .map(x => ({ ...x })) // avoid tainting the inputs | |
| .sort((a, b) => | |
| (a.visited = a.visited || (bears.sum = (bears.sum || 0) + a.hunger) || true) | |
| && | |
| (b.visited = b.visited || (bears.sum = (bears.sum || 0) + b.hunger) || true) | |
| && | |
| (a.name > b.name ? 1 : a.name < b.name ? -1 : 0)) | |
| .filter(b => b.hunger > bears.sum / bears.length) |
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 replaceRepeats = (input, number) => | |
| input.replaceAll( | |
| new RegExp(`${number}+`, "g"), | |
| x => x.length); | |
| // test | |
| [ | |
| replaceRepeats('1234500362000440', 0) === "1234523623441", | |
| replaceRepeats('000000000000', 0) == "12", |
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
| /* solution */ | |
| const validate = ([y, z, ...rest]) => | |
| rest.length | |
| ? (rest.shift() === y && validate([z, y, ...rest])) | |
| : true; | |
| /* testing */ |
NewerOlder