Skip to content

Instantly share code, notes, and snippets.

@jepras
jepras / Send Action Data to the Store
Created October 17, 2018 16:33
returning an action object in addNoteText action that feeds into notesReducer and returns associated text to type.
const ADD_NOTE = 'ADD_NOTE';
const notesReducer = (state = 'Initial State', action) => {
switch(action.type) {
// change code below this line
case ADD_NOTE:
return action.text
// change code above this line
default:
return state;
@jepras
jepras / Register a Store Listener
Created October 17, 2018 16:32
Used store.subscribe to invoke a callback function that simply adds +1 to count. Store.subscribe fires automatically when an action is dispatched.
const ADD = 'ADD';
const reducer = (state = 0, action) => {
switch(action.type) {
case ADD:
return state + 1;
default:
return state;
}
};
@jepras
jepras / Cash Register
Created October 11, 2018 07:00
Work in progress
function checkCashRegister(price, cash, cid) {
var change = 0;
var difference = cash - price;
console.log(difference)
// Create object with status & change key
var cashreg = {
status: "",
change: []
}
@jepras
jepras / Arguments optional
Created October 8, 2018 06:57
First creating a function to check if number. Then divide into if arg length is 2 or 1. If 1, return new function that takes in a second argument and then check + add.
function addTogether() {
// Function to check if a number is actually a number
// and return undefined otherwise.
var checkNum = function(num) {
if (typeof num !== 'number') {
return undefined;
} else
return num;
};
@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' />
)} />
);
@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 / 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) {
// 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 / 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;
}
@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;
}