Skip to content

Instantly share code, notes, and snippets.

View eday69's full-sized avatar

Eric Day eday69

  • Calgary, AB CANADA
View GitHub Profile
@eday69
eday69 / steamrollArray.js
Last active June 15, 2018 17:53
freeCodeCamp Intermediate Algorithm Scripting: Steamroller
// Flatten a nested array. You must account for varying levels of nesting.
function steamrollArray(arr) {
// I'm a steamroller, baby
return arr.reduce((acc, val) => Array.isArray(val) ? acc.concat(steamrollArray(val)) : acc.concat(val), []);
}
steamrollArray([1, [2], [3, [[4]]]]); // [1, 2, 3, 4]
steamrollArray([[["a"]], [["b"]]]); // ["a", "b"].
@eday69
eday69 / dropElements.js
Last active October 8, 2022 17:21
freeCodeCamp Intermediate Algorithm Scripting: Drop it
// Given the array arr, iterate through and remove each element
// starting from the first element (the 0 index) until the function
// func returns true when the iterated element is passed through it.
// Then return the rest of the array once the condition is satisfied,
// otherwise, arr should be returned as an empty array.
// Cannot use filter, since we need to return the rest of the array
// once the condition is met. We use a 'For' loop !
@eday69
eday69 / smallestCommons.js
Last active June 15, 2018 17:54
freeCodeCamp Intermediate Algorithm Scripting: Smallest Common Multiple
// 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.
// For example, if given 1 and 3, find the smallest common multiple of
// both 1 and 3 that is also evenly divisible by all numbers between 1
@eday69
eday69 / sumPrimes.js
Last active June 15, 2018 17:55
freeCodeCamp Intermediate Algorithm Scripting: Sum All Primes
// 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) {
let ourSum=0; // We start with one, prime for 1
@eday69
eday69 / sumFibs.js
Last active June 15, 2018 17:55
freeCodeCamp Intermediate Algorithm Scripting: Sum All Odd Fibonacci Numbers
// 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) should return 10 because all odd
// Fibonacci numbers less than or equal to 10 are 1, 1, 3, and 5.
@eday69
eday69 / convertHTML.js
Last active June 15, 2018 17:55
freeCodeCamp Intermediate Algorithm Scripting: Convert HTML Entities
// Convert the characters &, <, >, " (double quote), and ' (apostrophe),
// in a string to their corresponding HTML entities.
function convertHTML(str) {
// &colon;&rpar;
const convertHTML={ "&": "&amp;",
"<": "&lt;",
">": "&gt;",
"\"": "&quot;",
"'": "&apos;"}
@eday69
eday69 / uniteUnique.js
Last active June 15, 2018 17:56
freeCodeCamp Intermediate Algorithm Scripting: Sorted Union
// 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 array should not be sorted in numerical order.
@eday69
eday69 / fearNotLetter.js
Last active August 28, 2021 22:44
freeCodeCamp Intermediate Algorithm Scripting: Missing letters
// 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) {
let lastLetter=str.charCodeAt(0);
let missingLetter;
for (let i=1; i<str.length; i++) {
let currentLetter=str.charCodeAt(i);
console.log(lastLetter, currentLetter);
if ((lastLetter+1) < currentLetter) {
@eday69
eday69 / pairElement.js
Last active June 15, 2018 17:56
freeCodeCamp Intermediate Algorithm Scripting: DNA Pairing
// The DNA strand is missing the pairing element. Take each character,
// get its pair, and return the results as a 2d array.
// Base pairs are a pair of AT and CG. Match the missing element to the
// provided character.
// Return the provided character as the first element in each array.
// For example, for the input GCG, return [["G", "C"], ["C","G"],["G", "C"]]
@eday69
eday69 / myReplace.js
Last active June 15, 2018 17:57
freeCodeCamp Intermediate Algorithm Scripting: Search and Replace
// Perform a search and replace on the sentence using the arguments
// provided and return the new sentence.
// First argument is the sentence to perform the search and replace on.
// Second argument is the word that you will be replacing (before).
// Third argument is what you will be replacing the second argument
// with (after).