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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / 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) {
import axios from 'axios'
import AsyncStorage from '@react-native-community/async-storage'
import React, { useEffect, useState } from 'react'
import { Linking } from 'react-native'
import InAppBrowser from 'react-native-inappbrowser-reborn'
import { LoadingIndicator } from '../LoadingIndicator'
interface AuthPortalProps {
client_id: string
scopes: string[]
@Luke-Rogerson
Luke-Rogerson / Makefile
Created June 22, 2020 20:39
Setup TypeScript, eslint and prettier in a new JavaScript project
generate:
@echo "Adding TypeScript as dependency..."
@yarn add typescript -D
@echo "Creating tsconfig..."
@npx tsconfig.json
@echo "Adding eslint and prettier dependencies..."
@yarn add eslint prettier eslint-config-airbnb-typescript-prettier -D
@echo "Creating eslint config..."
@touch .eslintrc.js
@echo "module.exports = {\n extends: ['airbnb-typescript-prettier'],\n rules: {\n // just say 'no' to default exports!\n 'import/prefer-default-export': 'off',\n 'import/no-default-export': 'error',\n }\n };" >> .eslintrc.js