Skip to content

Instantly share code, notes, and snippets.

@jepras
jepras / DNA Pairing
Created September 29, 2018 08:06
Created a function that takes in a parameter, that is then looped and called until end. Function is a switch statement.
// Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch
// https://guide.freecodecamp.org/certifications/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/dna-pairing
function pairElement(str) {
var paired = [];
var search = function(char) {
switch (char) {
case 'A':
console.log("a found");
@jepras
jepras / Missing in sequence
Created September 29, 2018 08:30
Used charCodeAt to identify unicode point for char. In a while loop I compared the string version of the unicode alphabet with the char at the string.
function fearNotLetter(str) {
var c = 0;
var i = 0;
if (str) {
i = str.charCodeAt(0);
while (c < str.length && i < 122) {
if (String.fromCharCode(i) !== str.charAt(c)) {
return String.fromCharCode(i);
}
@jepras
jepras / Sorted Union
Created October 1, 2018 18:47
Using flat() to flatten from multiple arrays into 1. Using Set on flat array to only get unique values.
function uniteUnique(arr) {
var newArray = [];
for (let array of arguments) {
newArray.push(array);
}
var flatArray = newArray.flat()
var unique = [...new Set(flatArray)];
@jepras
jepras / Convert HTML Entries
Created October 1, 2018 19:07
Use switch to replace chars in a string. Use \ to escape characters.
function convertHTML(str) {
var htmlStr = "";
var search = function(char) {
switch (char) {
case '&':
console.log("& found");
str = str.replace("&", "&amp;");
break;
case '<':
@jepras
jepras / Fibonacci Numbers
Created October 1, 2018 19:29
Used while loop to not exceed value of num.
function sumFibs(num) {
var prevNumber = 0;
var currNumber = 1;
var result = 0;
while (currNumber <= num) {
if (currNumber % 2 !== 0) {
result += currNumber;
}
@jepras
jepras / Find prime numbers
Created October 2, 2018 05:39
Use a isPrime method to return true on primes. Then loop through and add result to output if isPrime.
function sumPrimes(num) {
function isPrime(num) {
if(num < 2) return false;
for (var i = 2; i < num; i++) {
if(num%i==0)
return false;
}
return true;
}
// Stolen from fCC solutions - could not figure it out
function smallestCommons(arr) {
// Sort array from greater to lowest
// This line of code was from Adam Doyle (http://github.com/Adoyle2014)
arr.sort(function(a, b) {
return b - a;
});
// Create new array and add all values from greater to smaller from the
@jepras
jepras / Drop it
Created October 2, 2018 06:34
Used a function as a input to check for Boolean. Used splice to remove first index in array.
function dropElements(arr, func) {
// Drop them elements.
for (var i = 0; i <= arr.length; i++) {
// Check console for current value
console.log("function is " + func(arr[0]));
console.log("value is " + arr[0])
// remove value from arr if function returns false
if(func(arr[0]) === false) {
@jepras
jepras / Truthy values
Created October 5, 2018 05:53
Used hasOwnProperty to check collection objects had the second argument in them to increase a counter. Finally check counter vs length to check if all values were true.
function truthCheck(collection, pre) {
// Is everyone being true?
var counter = 0;
for (let c in collection) {
if (collection[c].hasOwnProperty(pre) && Boolean(collection[c][pre])) {
counter++;
}
}
@jepras
jepras / React Authentication
Created October 5, 2018 19:25
If isAuthenticated, then render component. If not redirect to login.
const SecretRoute = ({ component: Component, ...rest }) => (
<Route {...rest} render={(props) => (
AuthService.isAuthenticated === true
? <Component {...props} />
: <Redirect to='/login' />
)} />
);