Skip to content

Instantly share code, notes, and snippets.

View jcolebot's full-sized avatar

Jonathan "JC" Coley jcolebot

View GitHub Profile
@jcolebot
jcolebot / sum-prime.js
Created May 2, 2022 19:52
freeCodeCamp: Sum All Primes Solution
// A prime number is a whole number greater than 1 with exactly two divisors: 1 and itself.
// For example, 2 is a prime number because it is only divisible by 1 and 2.
// In contrast, 4 is not prime since it is divisible by 1, 2 and 4.
// Rewrite sumPrimes so it returns the sum of all prime numbers that are less than or equal to num.
function sumPrimes(num) {
let primeArr = [];
for(let i = 2; i <= num; i++) {
if(primeArr.every((prime) => i % prime !== 0))
primeArr.push(i);
@jcolebot
jcolebot / spinal-case.js
Created April 15, 2022 14:19
freeCodeCamp: Covert a String to Spinal Case Solution
// Convert a string to spinal case. Spinal case is all-lowercase-words-joined-by-dashes.
// Regex + Join Method
function spinalCase(str) {
return str
.split(/[\s_]+|(?=[A-Z])/g)
.join("-")
.toLowerCase();
}
@jcolebot
jcolebot / name-value-pair.js
Created April 14, 2022 17:52
Matching Name and Value Pairs with Arguments
// Function looks through an array of objects and returns an array of all objects that have matching name and value pairs.
// Each name and value pair of the source object has to be present in the object from the collection.
// For example, if the first argument is [{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }].
// The second argument is { last: "Capulet" }. Then you must return the third object from the array (the first argument).
// Because it contains the name and its value, that was passed on as the second argument.
// Solution Using Filter and Every Methods
function whatIsInAName(collection, source) {
@jcolebot
jcolebot / seek-destroy.js
Created April 13, 2022 21:02
freeCodeCamp Seek and Destroy Solution
// You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments.
// Remove all elements from the initial array that are of the same value as these arguments.
// Note: You have to use the arguments object.
function destroyer(arr, ...args) {
return arr.filter(elem => !args.includes(elem))
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
@jcolebot
jcolebot / sum-all.js
Created April 13, 2022 14:27
freeCodeCamp: Sum All Numbers in a Range
// We'll pass you an array of two numbers. Return the sum of those two numbers plus the sum of all the numbers between them.
// The lowest number will not always come first.
// For example, sumAll([4,1]) should return 10 because sum of all the numbers between 1 and 4 (both inclusive) is 10.
function sumAll(arr) {
let total = 0;
if(arr[0] < arr[1]) {
for(let i = arr[0]; i <= arr[1]; i++) {
total = total + i;
}
@jcolebot
jcolebot / MultiplyBy11.js
Created January 31, 2022 22:12
Given a positive number as a string, multiply the number by 11 and also return it as a string without casting to an integer
// Multiply by 11
// Given a positive number as a string, multiply the number by 11 and also return it as a string. However, there is a catch:
// You are NOT ALLOWED to simply cast the numeric string into an integer!
// Now, how is this challenge even possible? Despite this, there is still a way to solve it, and it involves thinking about how someone might multiply by 11 in their head. See the tips below for guidance.
// Examples
// multiplyBy11("11") ➞ "121"
// multiplyBy11("111111111") ➞ "1222222221"
@jcolebot
jcolebot / FilterStringFromArray.js
Created January 31, 2022 16:14
Filter Out Strings from an Array
// Filter Out Strings from an Array
// Create a function that takes an array of non-negative integers and strings and returns a new array without the strings.
// Examples
// filterArray([1, 2, "a", "b"]) ➞ [1, 2]
// filterArray([1, "a", "b", 0, 15]) ➞ [1, 0, 15]
// filterArray([1, 2, "aasf", "1", "123", 123]) ➞ [1, 2, 123]
// Notes
// Zero is a non-negative integer.
// The given array only has integers and strings.
@jcolebot
jcolebot / README-Template.md
Created January 24, 2022 22:43 — forked from DomPizzie/README-Template.md
A simple README.md template

Project Title

Simple overview of use/purpose.

Description

An in-depth paragraph about your project and overview of use.

Getting Started

@jcolebot
jcolebot / timeConversionJava.java
Created January 19, 2022 19:37
Military Time Conversion Method in Java
Given a time in -hour AM/PM format, convert it to military (24-hour) time.
Note: - 12:00:00AM on a 12-hour clock is 00:00:00 on a 24-hour clock.
- 12:00:00PM on a 12-hour clock is 12:00:00 on a 24-hour clock.
Example
Return '12:01:00'.
@jcolebot
jcolebot / fibonacciGenerator.js
Last active February 24, 2021 18:25
Fibonacci Sequence solution using a for loop in JavaScript. Sequence grows by adding the sum of the two numbers that precede it. Remember to change the value of "n" when calling the function based on how long you want the sequence to be.
function fibonacciGenerator(n) {
var fibArray = []; //create empty array to push sequence into
if (n == 1) {
fibArray = [0]; //sets first element to 0
}
else if (n == 2) {
fibArray = [0, 1]; //sets second element to 1
}
else {