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
interface Admin { | |
id: string; | |
role: string; | |
} | |
interface User { | |
email: string; | |
} | |
function redirect(usr: Admin | User) { |
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
class Queue { | |
constructor() { | |
this.data = []; | |
} | |
add(n) { | |
this.data.unshift(n); | |
} | |
remove() { | |
return this.data.pop(); | |
} |
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
// Memoize to make more performant | |
function memoize(func) { | |
const cache = {}; | |
return function(...args) { | |
if (cache[args]) return cache[args]; | |
const result = func.apply(this, args); | |
cache[args] = result; | |
return result; | |
}; | |
} |
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
// --- Examples | |
// matrix(2) | |
// [[1, 2], | |
// [4, 3]] | |
// matrix(3) | |
// [[1, 2, 3], | |
// [8, 9, 4], | |
// [7, 6, 5]] | |
// matrix(4) | |
// [[1, 2, 3, 4], |
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 vowels(str) { | |
const vowels = /[aeiou]/gi; | |
return str.split('').filter(char => char.match(vowels)).length; | |
} |
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
// --- Examples | |
// pyramid(1) | |
// '#' | |
// pyramid(2) | |
// ' # ' | |
// '###' | |
// pyramid(3) | |
// ' # ' | |
// ' ### ' | |
// '#####' |
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
// --- Examples | |
// steps(2) | |
// '# ' | |
// '##' | |
// steps(3) | |
// '# ' | |
// '## ' | |
// '###' | |
// steps(4) | |
// '# ' |
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 capitalize(str) { | |
const capitalizedSentence = []; | |
for (let word of str.split(' ')) { | |
const capitalizedWord = word[0].toUpperCase() + word.slice(1); | |
capitalizedSentence.push(capitalizedWord); | |
} | |
return capitalizedSentence.join(' '); |
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 anagrams(stringA, stringB) { | |
return cleanString(stringA) === cleanString(stringB); | |
} | |
function cleanString(str) { | |
return str | |
.replace(/[^\w]/g, '') | |
.toLowerCase() | |
.split('') | |
.sort() |
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
// --- Directions | |
// Given an array and chunk size, divide the array into many subarrays | |
// where each subarray is of length size | |
// --- Examples | |
// chunk([1, 2, 3, 4], 2) --> [[ 1, 2], [3, 4]] | |
// chunk([1, 2, 3, 4, 5], 2) --> [[ 1, 2], [3, 4], [5]] | |
// chunk([1, 2, 3, 4, 5, 6, 7, 8], 3) --> [[ 1, 2, 3], [4, 5, 6], [7, 8]] | |
// chunk([1, 2, 3, 4, 5], 4) --> [[ 1, 2, 3, 4], [5]] | |
// chunk([1, 2, 3, 4, 5], 10) --> [[ 1, 2, 3, 4, 5]] |