Skip to content

Instantly share code, notes, and snippets.

View Luke-Rogerson's full-sized avatar

Luke Rogerson Luke-Rogerson

View GitHub Profile
@Luke-Rogerson
Luke-Rogerson / automaticTypeInterfence.ts
Created May 26, 2019 18:04
Use Javascript `in` operator for automatic type inference in TS
interface Admin {
id: string;
role: string;
}
interface User {
email: string;
}
function redirect(usr: Admin | User) {
@Luke-Rogerson
Luke-Rogerson / queue.js
Created December 18, 2018 23:08
Create a queue data structure. The queue should be a class with methods 'add' and 'remove'. Adding to the queue should store an element until it is removed
class Queue {
constructor() {
this.data = [];
}
add(n) {
this.data.unshift(n);
}
remove() {
return this.data.pop();
}
@Luke-Rogerson
Luke-Rogerson / fib.js
Created December 18, 2018 22:46
Print out the n-th entry in the fibonacci series. The fibonacci series is an ordering of numbers where each number is the sum of the preceeding two. For example, the sequence [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] forms the first ten entries of the fibonacci series. Example: fib(4) === 3
// 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;
};
}
@Luke-Rogerson
Luke-Rogerson / matrix.js
Created December 18, 2018 00:36
Write a function that accepts an integer N and returns a NxN spiral matrix.
// --- Examples
// matrix(2)
// [[1, 2],
// [4, 3]]
// matrix(3)
// [[1, 2, 3],
// [8, 9, 4],
// [7, 6, 5]]
// matrix(4)
// [[1, 2, 3, 4],
@Luke-Rogerson
Luke-Rogerson / vowels.js
Created December 17, 2018 12:41
Write a function that returns the number of vowels used in a string.
function vowels(str) {
const vowels = /[aeiou]/gi;
return str.split('').filter(char => char.match(vowels)).length;
}
@Luke-Rogerson
Luke-Rogerson / pyramid.js
Created December 17, 2018 12:24
Write a function that accepts a positive number N. The function should console log a pyramid shape with N levels using the # character. Make sure the pyramid has spaces on both the left *and* right hand sides
// --- Examples
// pyramid(1)
// '#'
// pyramid(2)
// ' # '
// '###'
// pyramid(3)
// ' # '
// ' ### '
// '#####'
@Luke-Rogerson
Luke-Rogerson / steps.js
Created December 16, 2018 21:43
Write a function that accepts a positive number N. The function should console log a step shape with N levels using the # character. Make sure the step has spaces on the right hand side!
// --- Examples
// steps(2)
// '# '
// '##'
// steps(3)
// '# '
// '## '
// '###'
// steps(4)
// '# '
@Luke-Rogerson
Luke-Rogerson / capitalize.js
Created December 16, 2018 18:58
Write a function that accepts a string. The function should capitalize the first letter of each word in the string then return the capitalized string.
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(' ');
@Luke-Rogerson
Luke-Rogerson / anagrams.js
Created December 16, 2018 18:30
Check to see if two provided strings are anagrams of each other. One string is an anagram of another if it uses the same characters in the same quantity. Only consider characters, not spaces or punctuation. Consider capital letters to be the same as lower case
function anagrams(stringA, stringB) {
return cleanString(stringA) === cleanString(stringB);
}
function cleanString(str) {
return str
.replace(/[^\w]/g, '')
.toLowerCase()
.split('')
.sort()
@Luke-Rogerson
Luke-Rogerson / chunk.js
Created December 13, 2018 08:25
Given an array and chunk size, divide the array into many subarrays where each subarray is of length size
// --- 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]]