Skip to content

Instantly share code, notes, and snippets.

Working from home

Nicki Hastings nickihastings

Working from home
View GitHub Profile
nickihastings / spinal-tap-case.js
Created Mar 23, 2018
Convert a string to spinal case. Spinal case is all-lowercase-words-joined-by-dashes.
View spinal-tap-case.js
function spinalCase(str) {
// "It's such a fine line between stupid, and clever."
// --David St. Hubbins
function processStr(match, offset, string){
//if the offset is not the first letter and there isn't already a hyphen,
//add a hyphen then change the letter to lowercase
if(offset !== 0 && string[offset-1] !== '-'){
return '-' + match.toLowerCase();
nickihastings / sum-odd-fibonacci-numbers.js
Created Mar 25, 2018
Given a positive integer num, return the sum of all odd Fibonacci numbers that are less than or equal to num. The first two numbers in the Fibonacci sequence are 1 and 1. Every additional number in the sequence is the sum of the two previous numbers. The first six numbers of the Fibonacci sequence are 1, 1, 2, 3, 5 and 8. For example, sumFibs(10…
View sum-odd-fibonacci-numbers.js
function sumFibs(num) {
//set up variables a counter to hold the next Fibonacci number and an
//array to hold all the Fibonacci numbers below num.
var count = 0;
var fib = [1, 1];
//loop through and keep adding Fibonacci numbers to the array until the num is reached
for(var i = 0; i<fib.length; i++){
count = fib[i] + fib[i+1];
nickihastings / sum-all-primes.js
Created Mar 26, 2018
Sum all the prime numbers up to and including the provided number. A prime number is defined as a number greater than one and having only two divisors, one and itself. For example, 2 is a prime number because it's only divisible by one and two. The provided number may not be a prime.
View sum-all-primes.js
function sumPrimes(num) {
var ints = [2]; //array to hold prime numbers
//function to test if a number is a prime
function isPrime(test){
for(var i = 2; i < test; i++){
if(test % i == 0){ //if there's no remainder it's not a prime
return false; //not a prime number
nickihastings / smallest-common-multiple.js
Last active Mar 30, 2018
Find the smallest common multiple of the provided parameters that can be evenly divided by both, as well as by all sequential numbers in the range between these parameters. The range will be an array of two numbers that will not necessarily be in numerical order. e.g. for 1 and 3 - find the smallest common multiple of both 1 and 3 that is evenly…
View smallest-common-multiple.js
function smallestCommons(arr) {
//sort the array ascending, so that a is always the smaller number
return a-b;
//first find the GCD of the pair of numbers using the Euclidean Algorithm.
//return the GCD
function gcd(a, b){
var x = a;
nickihastings / finders-keepers.js
Created Mar 30, 2018
Create a function that looks through an array (first argument) and returns the first element in the array that passes a truth test (second argument).
View finders-keepers.js
function findElement(arr, func) {
var num = 0;
//filter the array using the provided function, this will keep anything that returns true in an array;
num = arr.filter(func);
//return the first element in the new array.
return num[0];
findElement([1, 2, 3, 4], function(num){ return num % 2 === 0; });
nickihastings / drop-it.js
Last active Mar 30, 2018
Drop the elements of an array (first argument), starting from the front, until the predicate (second argument) returns true. The second argument, func, is a function you'll use to test the first elements of the array to decide if you should drop it or not. Return the rest of the array, otherwise return an empty array.
View drop-it.js
function dropElements(arr, func) {
// Drop them elements.
//shift() removes the element if it returns false
//so loop over the array, test against the function
//if it returns false remove the element, otherwise
//return the array.
while(arr.length > 0){
var check = func(arr[0]);
nickihastings / steamroller.js
Created Mar 31, 2018
Flatten a nested array. You must account for varying levels of nesting.
View steamroller.js
function steamrollArray(arr) {
// I'm a steamroller, baby
var result = []; //store the resulting flat array
//function to recursively flatten arrays inside of arrays.
function flatten(arr){
//for every item in the array check if it is an array itself
for(var i = 0; i < arr.length; i++){
if(Array.isArray(arr[i]) ){
//if it is an array call the flatten function and concatinate the results
nickihastings / binary-agents.js
Created Apr 2, 2018
Return an English translated sentence of the passed binary string. The binary string will be space separated.
View binary-agents.js
function binaryAgent(str) {
var arr = str.split(' ');
//var words = [];
for(var i =0; i<arr.length; i++){
arr[i] = parseInt(arr[i], 2);
arr[i] = String.fromCharCode(arr[i]);
//can also write the above as:
//words.push(String.fromCharCode(parseInt(arr[i], 2)));
//parses first then converts to letter then pushes to array.
nickihastings / everything-be-true.js
Created Apr 2, 2018
Check if the predicate (second argument) is truthy on all elements of a collection (first argument). Remember, you can access object properties through either dot notation or [] notation.
View everything-be-true.js
function truthCheck(collection, pre) {
// Is everyone being true?
//set a variable to count the truthies
var truthy = 0;
//loop over the array and test for true
//if it's true add one to truth
for(var i = 0; i<collection.length; i++){
truthy ++;
nickihastings / arguments-optional.js
Created Apr 3, 2018
Create a function that sums two arguments together. If only one argument is provided, then return a function that expects one argument and returns the sum. For example, addTogether(2, 3) should return 5, and addTogether(2) should return a function. Calling this returned function with a single argument will then return the sum: var sumTwoAnd = ad…
View arguments-optional.js
function addTogether() {
//create a function that test whether arguments are numbers
//if not a number return undefined, if it is return the number
function checkArgs(val){
if(typeof val !== 'number'){
return undefined;
return val;