Skip to content

Instantly share code, notes, and snippets.

View nickihastings's full-sized avatar
🏠
Working from home

Nicki Hastings nickihastings

🏠
Working from home
View GitHub Profile
@nickihastings
nickihastings / drop-it.js
Last active March 30, 2018 13:35
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.
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]);
if(!check){
@nickihastings
nickihastings / finders-keepers.js
Created March 30, 2018 10:58
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).
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
nickihastings / smallest-common-multiple.js
Last active March 30, 2018 10:40
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…
function smallestCommons(arr) {
//sort the array ascending, so that a is always the smaller number
arr.sort(function(a,b){
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
nickihastings / sum-all-primes.js
Created March 26, 2018 20:33
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.
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
nickihastings / sum-odd-fibonacci-numbers.js
Created March 25, 2018 19:09
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…
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
nickihastings / spinal-tap-case.js
Created March 23, 2018 20:42
Convert a string to spinal case. Spinal case is all-lowercase-words-joined-by-dashes.
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
nickihastings / convert-html-entities.js
Created March 23, 2018 18:48
Convert the characters &, <, >, " (double quote), and ' (apostrophe), in a string to their corresponding HTML entities.
function convertHTML(str) {
// &colon;&rpar;
//create an object to store the html conversions
var htmlEntities = {
'&' : '&amp;',
'<' : '&lt;',
'>' : '&gt;',
'"' : '&quot;',
"'" : '&apos;'
};
@nickihastings
nickihastings / sorted-union.js
Created March 22, 2018 20:40
Write a function that takes two or more arrays and returns a new array of unique values in the order of the original provided arrays. In other words, all values present from all arrays should be included in their original order, but with no duplicates in the final array. The unique numbers should be sorted by their original order, but the final …
function uniteUnique(arr) {
var args = [];
//loop over the arguments provided and then loop over each array
//within the arguments, check if the value is already in the new
//array, if it's not, add it in.
for(var i = 0; i < arguments.length; i++){
for(var j = 0; j<arguments[i].length; j++){
if(args.indexOf(arguments[i][j]) == -1){
args.push(arguments[i][j]);
}
@nickihastings
nickihastings / boo-who.js
Created March 22, 2018 20:39
Check if a value is classified as a boolean primitive. Return true or false. Boolean primitives are true and false.
function booWho(bool) {
// What is the new fad diet for ghost developers? The Boolean.
//check if the value 'bool' is strictly true or false, this means the value is a Boolean Primitive and should return true.
//Everything else should return false.
if(bool === true || bool === false){
return true;
}
else {
return false;
}
@nickihastings
nickihastings / missing-letters.js
Created March 21, 2018 20:46
Find the missing letter in the passed letter range and return it. If all letters are present in the range, return undefined.
function fearNotLetter(str) {
//in unicode A=65 and Z=90, a=97 and z=122
//get the starting letter's unicode, and then add one to check the next letter
var start = str.charCodeAt(0) + 1;
var missing = ''; //store missing letters
//loop over the string, get the letters unicode value and compare it to what the next letter's code should be:
for(var i = 1; i < str.length; i++){
//if the codes don't match append the letter to the variable
if(str.charCodeAt(i) !== start){