Instantly share code, notes, and snippets.

Chris West westc

View GitHub Profile
View getAbsUrl.js
function getAbsUrl(relUrl, baseUrl) {
// Make sure baseUrl ends in a forward-slash
baseUrl = baseUrl.replace(/^(\w*\:\/\/[^\/\?]+)([\?#]|$)/, '$1/$2');
var firstChar = relUrl.charAt(0);
if (firstChar === '?') {
return baseUrl.replace(/[\?#].*$|$/, relUrl);
}
if (firstChar === '#') {
return baseUrl.replace(/#.*$|$/, relUrl);
View isPrime-and-getKnownPrimes.js
(function() {
var primes = [1, 2, 3, 5];
isPrime = function(num) {
// If is not an integer or is not positive just return false.
if (num < 1 || num % 1) {
return false;
}
// See if it is found in the primes array.
View explore-tehmovies.js
// Navigate to any URL in the http://dl.tehmovies.pro/94/ domain and then run this code:
(function () {
var movies = [];
var urls = ['http://dl.tehmovies.pro/94/'];
function explore(rgxURL) {
var url = urls.shift();
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
View Google Searches.md
View Program.cs
using System;
using System.Net;
using System.Collections.Specialized;
using Newtonsoft.Json;
namespace SimpleSlackWebhook
{
public class Program
{
public static void Main(string[] args)
View Palindrome Chain Length.md

These are different solutions that I wrote for a problem that I was originally asked on an interview.

Recursive Solutions

Sequential Solutions

View pathsTo.js
function pathsTo(obj, fnTester, opt_limit, opt_searchLimit) {
opt_limit = +opt_limit || Infinity;
opt_searchLimit = ~~opt_searchLimit > 0 ? ~~opt_searchLimit : 1e5;
for (
var path, k, i, v, typeOfV,
objIndex = 0,
objs = [obj],
objsCount = 1,
paths = [[]],
View guessNumber.js
function guessNumber(maxNumber) {
var maxAttempts = Math.floor(Math.log(maxNumber) / Math.log(2)) + 1;
var answer = Math.floor(Math.random() * maxNumber) + 1;
var guess = '';
var guesses = [];
var message = '';
for (var attempts = 1; attempts <= maxAttempts; attempts = attempts + 1) {
guess = prompt(message + 'Enter a number between 1 and ' + maxNumber + ':', guess);
if (guess == undefined) {
View recursive-employees-with-subordinates.postgres.sql
WITH RECURSIVE subordinates AS ( -- CTE for employees with chain of command
WITH employees AS ( -- CTE for example employees
SELECT 1 AS id, 'Davy Crockett' AS full_name, NULL AS manager_id
UNION SELECT 2 AS id, 'Cindy Cruz' AS full_name, 1 AS manager_id
UNION SELECT 3 AS id, 'Harry Thompson' AS full_name, 1 AS manager_id
UNION SELECT 4 AS id, 'George Jefferson' AS full_name, 2 AS manager_id
UNION SELECT 5 AS id, 'Blinky Bill' AS full_name, 2 AS manager_id
UNION SELECT 6 AS id, 'Linda Vernsky' AS full_name, 1 AS manager_id
UNION SELECT 7 AS id, 'Abigail Quispe' AS full_name, 1 AS manager_id
UNION SELECT 8 AS id, 'Jessica Ericson' AS full_name, 7 AS manager_id
View recursive-cte-example.postgres.sql
WITH RECURSIVE subordinates AS ( -- CTE for employees with chain of command
WITH employees AS ( -- CTE for example employees
SELECT 1 AS id, 'Davy Crockett' AS full_name, NULL AS manager_id
UNION SELECT 2 AS id, 'Cindy Cruz' AS full_name, 1 AS manager_id
UNION SELECT 3 AS id, 'Harry Thompson' AS full_name, 1 AS manager_id
UNION SELECT 4 AS id, 'George Jefferson' AS full_name, 2 AS manager_id
UNION SELECT 5 AS id, 'Blinky Bill' AS full_name, 2 AS manager_id
UNION SELECT 6 AS id, 'Linda Vernsky' AS full_name, 1 AS manager_id
UNION SELECT 7 AS id, 'Abigail Quispe' AS full_name, 1 AS manager_id
UNION SELECT 8 AS id, 'Jessica Ericson' AS full_name, 7 AS manager_id