Skip to content

Instantly share code, notes, and snippets.

View TobiahRex's full-sized avatar
🛠️
Building ... 👷🏻‍♂️

Tobiah Rex TobiahRex

🛠️
Building ... 👷🏻‍♂️
View GitHub Profile
@TobiahRex
TobiahRex / screenBuilder-dynamicSentences.js
Last active January 27, 2020 18:17
Dynamic Sentences - Design Pattern
/**
@function generateRawDataFromSettings
receives differently shaped input data from various settings components,
containing users choices.
@param {FSA} settingsData
@return {object}
*/
/**
@TobiahRex
TobiahRex / gist:fbc5860104ea4c15b283ab98fbaf02b6
Last active April 25, 2019 02:16
Using useReducer with an initialization function in first arguments place.
import { v4 as generateId } from 'immutable';
//------------- CUSTOM HOOK -------------
export function useSetState(initialState) {
const [state, setState] = useReducer((prevState, action) => {
// HERE we can have normal reducer logic to handle the action and create a "newState"
if (someAction === someValue) {
if (!state.reqids.includes(action.meta.reqId)) return state;
// do something with the action.payload
}
// finally, return the new state
@TobiahRex
TobiahRex / gist:89d72ff0cfc3215c46d6482171e00c80
Last active April 25, 2019 02:02
getSingle Control Flow
import { v4 as generateId } from 'uuid';
function SomeModule() {
const [reqIds, setReqIds] = useState([]);
const [state, localDispatch] = useReducer(createReducer(reqIds), initialState);
function getSomeData(reqId) {
const newReqId = generateId();
setReqIds(newReqId)
@TobiahRex
TobiahRex / formatWords.js
Created January 28, 2019 18:09
Recursive Algo for word wrapping, given a fixed div width.
/* eslint-disable */
let result = '';
let test = "tobiah is happy eandhelovestocode fordaysanddaysanddays";
[
'tobiah',
'is',
'happy',
'eandhelovestocode',
'fordaysanddaysanddays'
]
@TobiahRex
TobiahRex / crud-server.js
Created November 3, 2018 11:34
CalHacks-Node-Server-Crud-Template
/*
REMEMBER:
1. Restart server when you write new Logic changes.
2. Run server with "node <name of file>.js"
3. Add npm packages with "yarn add <package name>" (Make sure Yarn is globally installed)
*/
const express = require('express');
const fs = require('fs');
const morgan = require('morgan');
@TobiahRex
TobiahRex / possible-paths.js
Created October 29, 2018 06:25
Solution to Possible-Paths from CCI
const gcd = (n1, n2) => {
if (n2 === 0) return n1;
return gcd(n2, n1 % n2);
}
const main = (input) => {
if (gcd(input[0], input[1]) == gcd(input[2], input[3])) {
return 'YES';
} else return 'NO';
}
@TobiahRex
TobiahRex / string-rotation-linked-list.js
Created October 28, 2018 04:38
String Rotation Solution from CCI - Linked List implementation.
/*
String Rotation: Assume you have a method isSubstring which
checks if one word is a substring of another.
Given two strings, sl and s2,
write code to check if s2 is a rotation of sl
using only one call to isSubstring
(e.g., "waterbottle" is a rotation of"erbottlewat").
*/
var express = require('express')
var app = express()
var http = require('http').Server(app)
app.use(express.static(__dirname))
app.get('/', function(req,res){
res.sendFile(__dirname + '/index.html')
})
const WebSocket = require('ws');
@TobiahRex
TobiahRex / parseHackerRank.js
Created October 10, 2018 00:55
HackerRank-Parse-Input
/*
HackerRank standardizes its input stream for many of its challenges.
This function can be optimized to account for different input types.
Simply change variable names etc.
*/
function parseInputs(input) {
const inputs = input.split('\n');
const testCases = Number(inputs[0]);
@TobiahRex
TobiahRex / dijkstra.js
Created June 9, 2018 03:43 — forked from stella-yc/dijkstra.js
Dijkstra's Algorithm in Javascript using a weighted graph
const problem = {
start: {A: 5, B: 2},
A: {C: 4, D: 2},
B: {A: 8, D: 7},
C: {D: 6, finish: 3},
D: {finish: 1},
finish: {}
};
const lowestCostNode = (costs, processed) => {